2 Replies Latest reply on Feb 16, 2010 9:08 AM by lcc1227

# Rounding a percent to a whole number

Hello,

I'm still fairly new to performing script calculations and with this particular one seems a bit more complex for me.

I'm trying to determine the percent difference between 2 numbers and display the percentage as a whole number. Logically my calculation looks like

(numericField1 - numericField2 / numericField1 * 100)
(ex. 1000 - 850 / 1000 * 100 = 15%)

I need assitance with my calculation and especially rounding the % to a whole number. I would not like any answers displayed like 22.66666%

The code below is somethig I tried

event.value = roundNumber( ( this.getField(numericField1).value - this.getField(numericField2t).value  )  / this.getField(numericField1).value , 2 ) + '%';

function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;

• ###### 1. Re: Rounding a percent to a whole number

Actually Adobe makes this really simple for you.  If you use a numeric field to display the result, the display pattern for the field will control how the number is shown.  You can set a percentage pattern:"num{zzzz9.99%}" to control how many digits will be displayed, and how many digits will be significant past the decimal point.  If there are more digits, Adobe will automatically round the number.  In the attached example I used the pattern

"num{zzzz9%}" to ensure that only a whole number will be displayed.  Adobe will use normal rounding rules: 0-4 rounds down, 5-9 rounds up.

Additionally, the percentage pattern will automatically multiply the field value by 100, so you don't need to do that in your script.

For the example attached, I added your calculation as FormCalc script to the Calculate event of the result field.  The script is now simply:

(NumericField1 - NumericField2) / NumericField1

Adobe takes care of the rest.

• ###### 2. Re: Rounding a percent to a whole number

This adds a lot more clarity for me! Thank you so much!!!