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

Formula to round up to nearest even penny

David Dunn
Registered: Oct 28 2010
Posts: 116
Answered

I am searching for a JavaScript math function to round a currency figure UP to the nearest even penny, e.g. $123.43 would round up to $123.44. I do need the rounding to an even number in the second decimal place, not rounding to an even integer.
 
I found an Excel function (MOD) that accomplishes my objective perfectly, but it is not recognized in JavaScript. Here is an example of the Excel formula, where MOD is the function not recognized.
 
var N3 = this.getField("num.Tax.PriorYear.Estimate").value
 
event.value = N3 +MOD(N3, 0.02)
 
MY QUESTION: what JavaScript function is equivalent to the "MOD" function provided in Excel, and what is the proper syntax.
 
ALSO: can someone refer me to a good reference guide for JavaScript mathematical functions?
 
Thank you in advance for your help.
 
DavidD

David D

My Product Information:
Acrobat Pro 9.4, Windows
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
The % symbol is the modulus operator for JavaScript. So you can do:

var N3 = +getField("num.Tax.PriorYear.Estimate").value;
var result = N3 + (N3 % 0.02);
event.value = util.printf("%.2f", result);


My favorite JavaScript reference is Flanagan's "JavaScript: The Definitive Guide": http://www.amazon.com/JavaScript-Definitive-Guide-David-Flanagan/dp/0596101996

The ECMAScript Specification, though a bit dry, is useful too: http://www.ecma-international.org/publications/standards/Ecma-262.htm