Copy link to clipboard
Copied
Could someone help me figure out how to round to 2 decimal places?
I've tried value= int((v2)*100/100; and other formulas that don't seem to be working.
I have managed to get the code to calculate as well as leave the box blank if mgkgpremed1 is blank by using the following in the format -> custome java script box:
var v1 = +this.getField("mgkgpremed1").value;
var v2 = +this.getField("mgpremed1").value;
if (v1==0) event.value="";
else event.value = kg*v1
And in the calculate section it has the product of kg*mgkgpremed1
Now I would also like v2 (or mgpremed1) to round to 2 decimal places. How do I do this? Would it go in the same format box?
Copy link to clipboard
Copied
You can use this function to do it:
function myRound(v, nDecimals) {
return Math.round(v * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
}
Copy link to clipboard
Copied
And you can control whether you want it to round up or down by changing Math.round (which goes to the nearest integer), to Math.ceil or Math.floor, respectively.
Copy link to clipboard
Copied
Thank you, I will try this. I really appreceiate the help
Copy link to clipboard
Copied
There are actually a few options for this. For example,
event.value = event.value.toFixed(2);
Or to create a rounded string.
event.value = util.printf("%,0.2f",event.value);
Copy link to clipboard
Copied
I did do this in the end, I found it after I posted. But as it just truncates the number after 2 decimal places, I was hoping to see if there was a way to round.
Thank you
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi,
If you're new to JavaScript (or even an experienced user)it seems like we all refer to rounding decimals incorrectly.
JavaScripting experts in the forums will ask you to specify if you really need to round up or down your decimal values with precission, or, to actually truncate the face value to only 2 digits to the right of the decimal period, simply because you won't be able to round up the real value with this method.
I am making this distinction because I've been corrected by ACP's and MVPs in the forums before. And I want to make sure I give you the appropriate answer.
For example, you can use a line of script like this for the event.value:
event.value = util.printf("%.2f", event.value);
This will resolve your immediate issue. But when you click with your mouse on that field, you'll notice that the real total decimal value is displayed.
Is this solution is what you're looking for?
Otherwise the real explanation to precission rounding is as imperfect as imperfect could be, and a real pain in the neck to work around with JavaScript.
A great explanation to this rounding problem is explained in great detail here:
Copy link to clipboard
Copied
sorry guys,
I just noticed you already replied when I hit my reply button. Seems like I was typing my answer at the same time.
Copy link to clipboard
Copied
Thank you for that explanation. I will try the options and see what works best for the form. I did find that formula to truncate the decimals at 2, but I'll see what gives the best results.
I am beginner at best!
Thank you for the reply, I really appreciate it
Copy link to clipboard
Copied
Your welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now