Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Round to 2 decimal places

Community Beginner ,
May 31, 2020 May 31, 2020

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?

TOPICS
General troubleshooting , How to , PDF forms
8.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2020 May 31, 2020

You can use this function to do it:

 

function myRound(v, nDecimals) {
	return Math.round(v * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2020 Jun 02, 2020

Thank you, I will try this. I really appreceiate the help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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);

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2020 Jun 02, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2020 Jun 02, 2020
LATEST
Both options I posted do round the value. But if you are having issue with it, then Try67 posted a mathematical formula for rounding.
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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:

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2020 Jun 02, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2020 Jun 02, 2020

Your welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines