5 Replies Latest reply on Feb 12, 2014 12:19 PM by George_Johnson

# Calculate Percentage

Hi,

I have 2 currency Field "Charged" and "Reduction", im trying to get the percentage difference between them in a Javascript format.

• ###### 1. Re: Calculate Percentage

What formula do you want to use for this calculation exactly? For example:

100 * (Charged - Reduction) / Charged

or

| (Charged - Reduction) | / ((Charged + Reduction) / 2)

or something else?

and what should happen if any of the inputs fields are blank?

• ###### 2. Re: Calculate Percentage

if the input fields are blank the pecentage field should be blank too or at 0%. is there a special fomula for that?

and thank you for your help.

• ###### 3. Re: Calculate Percentage

OK, it would probably be best to make it blank. You didn't say what formula you want to use for the percent difference., so I can't suggest any specific code.

• ###### 4. Re: Calculate Percentage

Ok, and I used the first formula

• ###### 5. Re: Calculate Percentage

The custom calculation script could be:

// Custom calculation script

(function () {

// Get the field values, as strings

var s1 = getField("Charged").valueAsString;

var s2 = getField("Reduction").valueAsString;

// Perform the calculation if both fields are not blank

// and Charged is not zero

if (s1 && s2 && +s1 !==  0) {

event.value = 100 * (+s1 - +s2) / +s1;

} else {

event.value = "";

}

})();