Copy link to clipboard
Copied
I'm struggling trying to resolve a rounding issue. Basically on the form I set up, when to lines are added together sometimes because of rounding it throws off the total by one penny. Really frustrated that I can figure out how to resolve it with Java Script. Can anyone take a look at the script below and possibly offer any scripting that will give a result of two decimal places as an answer? So, for example i may have result that comes up as 82.215. In this case I'd like it to default a result to 82.22. Here is the current script:
var theField = this.getField("l6A");
var theValue = theField.value;
if (theValue > 0.01) {
this.getField("l6").value= (theValue * 0.90);
}
else {
this.getField("l6").value="";
}
Thank you in advance for any ideas!
Is off because your script is not employing the correct method.
If you're trying to keep a number value to two decimal points, You must use the float format to handle the decimal fractions properly and in accordance with the decimal rounding rules. Otherwise, the term rounding, is being employed incorrectly in the context of your current script.
Change this line :
this.getField("l6").value= (theValue * 0.90);
To:
this.getField("l6").value = util.printf("%.2f", (theValue * 0.90));
...
Copy link to clipboard
Copied
Did you format the field "l6" as a number with 2 decimals?
Copy link to clipboard
Copied
Many people don't realise how demanding it can be to program for money calculations (at least, not until the tax authorities come to "investigate" a discrepancy). And I see you are on top of the result you need.
what you need to do, to avoid rounding errors from partial pennies, is make sure EVERY figure is forced to a whole number of pennies when it is entered or calculated. You cannot fix this only on the final total. This is a lot of little scripts. Personally I would prefer to keep all figures in whole pennies like 1234 and only turn to 12.34 for display. Still need to round every input and calculation to a whole number. You might consider this too much.
Copy link to clipboard
Copied
Unfortunately I'm being asked to keep it to 2 decimal points and because of third decimal makes it round up or down to the next cent it is throwing things off by a penny.
Copy link to clipboard
Copied
Is off because your script is not employing the correct method.
If you're trying to keep a number value to two decimal points, You must use the float format to handle the decimal fractions properly and in accordance with the decimal rounding rules. Otherwise, the term rounding, is being employed incorrectly in the context of your current script.
Change this line :
this.getField("l6").value= (theValue * 0.90);
To:
this.getField("l6").value = util.printf("%.2f", (theValue * 0.90));
See additional tips in this other recent discussion:
Copy link to clipboard
Copied
AWSESOME! That worked. THANK YOU so much!
Copy link to clipboard
Copied
You're welcome.