• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Am I able to limit the decimal places in a JavaScr...

# Am I able to limit the decimal places in a JavaScript calculation?

New Here ,
Jun 07, 2019 Jun 07, 2019

Copied

I have used the following code in a pdf document.    event.value = ( this.getField("Rent").value / this.getField("Month").value ) * this.getField("Days").value. I have formatted the field for two decimal places. When I total several of these calculations in a later field it is sometimes off by .01. While the field is rounding the number, it is using the actual number with many places after the decimal point in adding multiple fields together.

TOPICS
Acrobat SDK and JavaScript

Views

2.3K

Report

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

Community Expert , Jun 07, 2019 Jun 07, 2019

Formatting controls the appearance of the field data, but the value is the same.

So you need to change the decimal places in the actual field value.

Try this.

event.value = Number(util.printf("%0.2f",nValue));

This code code uses the print function to create a string version of the number with the correct number of digits, and then converts it to a number.

From a coding perspective this is easier than trying to use math to cut off the decimal places. Programming p

...

3 Replies 3
Community Expert ,
Jun 07, 2019 Jun 07, 2019

Copied

You must round the calculated value.

Report

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

Copied

Formatting controls the appearance of the field data, but the value is the same.

So you need to change the decimal places in the actual field value.

Try this.

event.value = Number(util.printf("%0.2f",nValue));

This code code uses the print function to create a string version of the number with the correct number of digits, and then converts it to a number.

From a coding perspective this is easier than trying to use math to cut off the decimal places. Programming purest would have a problem with it.

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 07, 2019 Jun 07, 2019

Copied

LATEST

Thank you Thom. This worked the magic I needed.