These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Help....need rounding calculations

tanorman75
Registered: Aug 5 2010
Posts: 7
Answered

Please help...this is my first attempt at creating a form that has calculations.

I'm able to perform the simple calculations with little problem, however - I am having difficulty with the fields not rounding up to the nearest whole number (although I have set it up to dispaly zero decimal places).

For example, I have a field that divides by 4.5 - and I need the total to round to the nearest whole number. Can anyone please share a simple solution?

My Product Information:
Acrobat Standard 9.3.1, Windows
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
To override Acrobat's default behavior, you will need to use a custom calculation script, like so:
event.value = Math.round(this.getField("field1").value / 4.5)

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
For a more generalized solution, I would use a function to perform the rounding. There is a known issue with JS 'Math.round' for certain values. For example multiply 4.935 by 100 and round to decimal places.

But one can use the 'util.printf()' method to also provide a rounding action. One can create a document level function that can be used by any calculation within the form.
function Round(fNumber, fDec) {/*Purpose: Round a number to a given number of decimal places

 Parameters:

fNumber - number to be rounded

fDec - number of decimal places to round to.

If omitted zero decimal places is assumed.

Function will work just like Math.round method

 Returns number rounded to given number of decimal places

*/
 // check to see fDec passedif(typeof fDec == 'undefined') {// fDec parameter does not existfDec = 0; // force value to zero} // check to make sure only number passedif ( isNaN(fNumber) )app.alert('Value passed to round is not a number: ' + fNumber, 1, 0);if ( isNaN(fDec) )app.alert('Value passed for number of decimals is not a number: ' + fNumber, 1, 0); fDec = Math.floor(fDec); // only allow whole numbers for decimals // return the rounded nubmerreturn util.printf('%,1 .' + fDec + 'f', fNumber);} // end of Round funciton

George Kaiser

tanorman75
Registered: Aug 5 2010
Posts: 7
Thank you both for your assistance...I think that the problem is solved (I wasn't validating)...which seems to have led to another problem....

Field_9 is the one that I am dividing by 4.5 (which now seems to be rounding to a whole number).

Now the problem is with the next field. Field_10 is suppossed calclulate to add 1 (one) to Field_9 (for example, if field 9 calculates at 2714 - then field_10 should automatically become "2715".

My simple calculation is : Field_9 + 1

The problem is that Field_10 does not automatically calculate when entering amounts. I have to enter zero (or any other key) in that field before it automatically calculates.

What am I not doing here?

In advance, thanks for your assistance!
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
Check the field calculation order. Field_10 should be below Field_9 in the list.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com