Copy link to clipboard
Copied
I've got several fields that calculate a * b= c. I'm having a heck of a time trying to add these numbers and round up the total to only 2 decimal points (e.g. 1.625 + 1.3264 = 2.9514) total should be 2.95. Can anyone help me?
Thanks,
Deb
Copy link to clipboard
Copied
You can use this code as the custom calculation script for "c":
var a = Number(this.getField("a").value);
var b = Number(this.getField("b").value);
event.value = (a+b).toFixed(2);
Copy link to clipboard
Copied
I'm still having trouble. Here's what I'm trying to do:
115.5*1.15 = 132.825 (the total rounds up and displays 132.83)
195.25*.95 = 185.4875 (the total rounds up and displays 185.49)
The total shows 318.31
I want the displayed numbers in BOLD to total 318.32
Copy link to clipboard
Copied
You need to better define the logic of when it should round up and when down, then...
Copy link to clipboard
Copied
Ah, you want each individual number to be rounded as well?
In that case, you can do it like this:
var a = Number(this.getField("a").value).toFixed(2);
var b = Number(this.getField("b").value).toFixed(2);
event.value = (Number(a)+Number(b)).toFixed(2);
Copy link to clipboard
Copied
I really appreciate your help... My first set of calculations do round up. However, when it totals, it is using the actual number (132.825) instead of the rounded up number (132.83) etc. so my total still total shows 318.31 instead of 318.32. I've attached a picture to explain. ![]()

Copy link to clipboard
Copied
You can use the round function:
Copy link to clipboard
Copied
There are many ways to accomplish this, some are better than others.
One can use the util.printf method to round the result.
Then there is the toFixed method in JavaScript.
There is the Math.round method in JavaScript, but this is for integers and not floating point numbers, but if one uses the method twice, once to round to a higher level of precision and then to the final precision it should work. This is sometimes referred to as double rounding.
D.P. Story has posted Some Exercises in Rounding on the AcroTex PDF Blog. He includes a document level function to round floating point numbers to a specified value and can round to whole numbers or decimals. Below is the function with a minor change from the original post.
function myRound (n, d)
{
n = String(n).replace(/,/g,"");
d = -1*d
var m = Math.pow(10,d);
n *= m;
n = Math.round(n);
n /= m;
d = ( d > 0 ) ? d : 0;
n = n.toFixed(d);
return n;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now