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

Adobe Acrobat Document Level Script to Round to Two Decimal Places?

New Here ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Good morning,

 

 

I have failed in my attempt to find a document-level JavaScript that will make Acrobat round to two decimal places, without having to set the formatting to "number - two decimal places" manually for each text field, or to add a "validate" code to each field.

Any help would be greatly appreciated!

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

575

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 ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Here's are 2 different ways to round numbers. Each has a different purpose.

 

1)  Rounding a number to a number with fixed number of decimal places. This techniqe keeps the value as a number. It's used in calculations and other value type of operations. 

 

var nMyNumber = 3.666;  // just a number for the example

 var nRoundedNumber = nMyNumber.toFixed(2);  // Round to two decimal places

 

2) Conver number to a string, rounded to a fixed number of decimals. This technique is used for presenting the number value, not using it in other calculations. For example, in a format script, or when the number is placed in a larger text string.

 

var nMyNumber = 3.666;  // just a number for the example

 var strRoundedNumber = util.printf("%0.2f",nMyNumber);  // String presentation of number rounded to two decimal places

 

 

      

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

Votes

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 ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Thank you so much, it worked! Is there any way to make it run continuously, as opposed to just running when opening the document? I have radio groups that calculate averages, but the script only worked when I closed the file and opened it up again.

Votes

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 ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

So what exactly are you trying to do?  To what is this rounding script applied?

 

 

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

Votes

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 ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

I have several radio groups, with buttons assigned from 1 - 5. Text boxes then average the groups. I was hoping the text boxes could round the groups, without my having to individually format or validate them.

 

Also, I noticed the script var strRoundedNumber = util.printf("%0.2f",nMyNumber); doesn't round up, is there any way to instruct it to do so?

 

I also have a button that turns the calculations off and on. I tried adding the script to this button, but got an error that "nMyNumber" was not defined.

 

Thank you for your help!

Votes

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 ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

LATEST

The "util.printf" code rounds up for me.  Do you have an example that doesn't work? 

 

The "nMyNumber" variable was just for the example code. Of course, in the real code it has to be replaced with the value that needs to be rounded. 

 

 

There are two different locations where the result of a calculation should be rounded. Each has a different purpose.

1. In the calculation script.  Do this is you need the number to be rounded for further operations. Could also be done for formatting if this is the end result. But could be problematic. 

2. In a format script for presenting the number. 

 

If the formatting/calculation is exactly the same for several fields, then it is a good practice to generalize the script so that it will work the same for all the fields where it is applied. Then put this code into a document level function.   Then, call the function from the appropiate event on each of the fields. 

 

Take a look at this article. In particular, read the section on Document Scripts:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

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

Votes

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