6 Replies Latest reply on Jul 12, 2013 1:22 PM by RCHouston1

# Calculate the % of Change

I need to calculate the % change from A to B.  I've used the Simplified field notation and came up with the correct value:

(A-B)/B

However, I need this value to be formatted as a percentage and "A" and "B" are formatted as currency.  I formatted the field and I get the error "The Value Entered does not match the format of the field [field name]"

When I change the format to "none" and change the formula to ((A-B)/B)*100 I get the correct value with no errors however I don't get the % sign and I have no control over decimal places.

Common issues I've seen in researching this is field names and all mine are alpha characters.

Is it possible to use the SFN with multiple field formats? If not, how do I write this in JavaScript?

• ###### 1. Re: Calculate the % of Change

Try setting the format of the field to "None" and clear the form or enter a value into the "A" field and observe what happens in the result field.

Is the "B" value empty or zero?

There have been many posts about how to script a calculation that involves division.

Search for division by zero

• ###### 2. Re: Calculate the % of Change

No, both fields contain numerical data.

We have noticed that we can get this to work properly until we try to clear the fields or save the form. Once the error starts popping up it does it for all fields in the form.

• ###### 3. Re: Calculate the % of Change

When a field is empty (like when you reset the form), its value is

considered to be zero by the internal code that does the calculations.

Hence the error since you're trying to divide by zero, which is an illegal

operation.

You need to do division using a custom script.

• ###### 4. Re: Calculate the % of Change

Thank you.

Off to research how to do this.  The resources I have here have not been very helpful.  I appreciate the advice!!!!

• ###### 5. Re: Calculate the % of Change

For the field to hold the result. Set the Format to 'Percent" and use the following "Custom JavaScript":

var A = Number(this.getField("A").value);

var B = Number(this.getField("B").value);

event.value = ""; // default if no calculation performed;

if(B != 0) {

// if value of B is not zero or null perform calculaiton;

event.value = (A - B) / C;

} // end of calculation;

The result will show 0% with the number of decimal places if the field is empty.

You could also modify the script to only perform the calculation if both input values have a non-null or non-zero value.

• ###### 6. Re: Calculate the % of Change

YOU ARE THE BEST!!!  Works great.  Thank you so much for your assistance.