6 Replies Latest reply on Jul 10, 2015 11:35 AM by guptamax2015

# using script to get percentage values and not able to sum upto 100%

Hello All

I am trying to use a parseFloat() function to convert string to percentage value and then trying to add all of the % values.

but for few scenrios even if the sum is 100%, from the function i get the PctValue either < or > 100.00%

function doing the calculation is as follows -

code -

iterating_through_all_the_fields {

PctValue += parseFloat(this.getField("Percentage1").valueAsString);

}

sample -

11.12

11.11

11.11

11.11

11.11

11.11

11.11

11.11

11.11

expected sum is 100

but through above mentioned code i get back 99.99

Any help is appreciated in this regard.

• ###### 1. Re: using script to get percentage values and not able to sum upto 100%

Instead of parseFloat() use Number().

On Fri, Jul 10, 2015 at 6:24 PM, guptamax2015 <forums_noreply@adobe.com>

• ###### 2. Re: using script to get percentage values and not able to sum upto 100%

The actual numeric result of that calculation in JavaScript will be: 99.99999999999999

So if you round correctly, the result can be 100.00.

• ###### 3. Re: using script to get percentage values and not able to sum upto 100%

This is a result of using the IEEE floating point standard and converting of decimal values to binary values. One should round for all money calculations or the mils will build up and cause errors. For percentages it is best to round the inputs and the final total to eliminate the accumulation of the amounts below the least significant digit.

It is possible to use the util.printf for rounding.

The following script will shows what is happening:

function Round(nValue, nDec) {
if(typeof nDec == "undefined") nDec = 0;
return Number(util.printf("%,1 0." + nDec + "f", Number(nValue)));
}

var nValues = new Array(11.12, 11.11, 11.11, 11.11, 11.11, 11.11, 11.11, 11.11, 11.11);
var nSum = 0;
for(var i = 0; i < nValues.length; i++ ) {
nSum = nSum + Round(nValues[i], 2);
console.println(i + " " + nValues[i] + " " + nSum);
}
console.println(nSum);
nSum = Round(nSum, 2);
console.println(nSum);

Progression of the sum at each step.
0 11.12 11.12
1 11.11 22.229999999999997
2 11.11 33.339999999999996
3 11.11 44.449999999999996
4 11.11 55.559999999999995
5 11.11 66.66999999999999
6 11.11 77.77999999999999
7 11.11 88.88999999999999
8 11.11 99.99999999999999
99.99999999999999
100

true

It is a good idea to use the decimal values for a percentage and let the form field's format resolve the display and rounding. But for sum applications the user may want to round the actual field value and not just the display.

• ###### 4. Re: using script to get percentage values and not able to sum upto 100%

I tried using Number() but it still gives 99.99

• ###### 5. Re: using script to get percentage values and not able to sum upto 100%

Regardless of how you convert to a number, the result of the calculation will be the same. You need to round the result.

• ###### 6. Re: using script to get percentage values and not able to sum upto 100%

Thanks. util.printf() works magic.. thanks a lot.