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

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

New Here ,
Jun 07, 2019 Jun 07, 2019

Copy link to clipboard

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

1.2K

Likes

Translate

Translate

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 guidelines

correct answers 1 Correct answer

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.

var nValue = ...Your calculation...

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

...

Likes

Translate

Translate
Community Expert ,
Jun 07, 2019 Jun 07, 2019

Copy link to clipboard

Copied

You must round the calculated value.

Likes

Translate

Translate

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 guidelines
Community Expert ,
Jun 07, 2019 Jun 07, 2019

Copy link to clipboard

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.

var nValue = ...Your calculation...

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

Likes

Translate

Translate

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 guidelines
New Here ,
Jun 07, 2019 Jun 07, 2019

Copy link to clipboard

Copied

LATEST

Thank you Thom. This worked the magic I needed.

Likes

Translate

Translate

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 guidelines