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

rounded numbers

smitchell15
Registered: Mar 15 2011
Posts: 3
Answered

Hi All,
 
I was hoping someone would be able to help me with the following problem.
 
I have 3 text fields called "Text4" , "Text5" and "Text6"
Text4 is a Nett Total TextField
Text5 is a VAT TextField
Text6 is a Gross Total textField
 
"Text6" has a script as follows:
var a = this.getField("Text4").value;
var b = this.getField("Text5").value;
event.value = a+b;
 
so far so good! But...
when Text4 value is 108.41399999999999 //this displays in PDF as £108.41
Text5 value is 16.262099999999997 //this displays in PDF as £16.26
Text6 value is 124.67609999999999 //this displays in PDF as £124.68
 
what Text6 should say is £124.67!
 
How can i get round this, i know i need to use the math.round but am not sure how to apply this and i'm now stuck. Any help would be greatly appreciated.
 
Cheers all

My Product Information:
Acrobat Pro 9.4.2, Windows
Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
Hi,

set the Text6 properties like that: http://pix.am/GW8z.png

;-)
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
You can use the toFixed method (which is actually what Adobe uses to display the formatted numbers (the + in the beginning is to convert the formatted string back to a number).

var a = +this.getField("Text4").value.toFixed(2);
var b = +this.getField("Text5").value.toFixed(2);
event.value = a+b;

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

smitchell15
Registered: Mar 15 2011
Posts: 3
Thanx try67, i added in the code and it worked a treat! :-)

Thank you very much

Cheers
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you set the format for a field to "Number" and specify the decimal places, the displayed value will be rounded to the specified precision, but the actual number will be the entered text or the computed value with no rounding.

JavaScript provides a couple of methods for rounding or truncating numbers, but they only work at the whole number level so one needs to manipulate the decimal point if one wants to round to values with a precision of less than 1.

JavaScript has the 'parseInt' core function, and the Math object has the ceil, floor, and round methods, and the Number constritor has the toFixedtoPrecision . Acrobat JavaScript has the 'util.printf()' method that also can round, but rounds with values less than 1..It is well known that the JavaScript Math.round method does not always properly round a number. A solution presented by D. P. Story:

function myRound (n, d) {
n = n.toString().replace(/,/g,""); // convert number to string & remove comas
d = -1 * d // convert précising to exponent for power of 10 adjustment
var m = Math.pow(10, d); // adjustment for decimal places
n *= m; // adjust decimal places of number to round
n = Math.round(n); // round to integer
n /= m; // shift decimal back to original location
d = ( d > 0 ) ? d : 0; // set précising if precision is less than 1
n = n.toFixed(d); // convert to fixed number with précising decimal places
return n; // return rounded value
}

You use -2 for rounding to the hundreths, 1/100.

You should realize that when one applies the rounding can affect the displayed result. Rounding the values that go into a calculation can produce a different result than not rounding them. In commerce one might want to round the result of a calculation before using that computed value in another calculation.

How is the Net Total computed?
What is the VAT rate?
How do you calculate the VAT amount?

I would show the VAT or any tax rate along with the computed tax amount.

You might want to round the Net Total and the VAT Amount as part of the calculation for those fields.

George Kaiser