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

Simplified field notation calculation rounding in PDF form

New Here ,
Dec 02, 2021 Dec 02, 2021

Hey, Hoping someone can help me. I am tring to create a field notation where it pulls the salary (from another field) and rounds it up to the nearest $100. Do I need to use java script? Please help!

TOPICS
Create PDFs , General troubleshooting , How to , JavaScript , PDF forms
1.6K
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 ,
Dec 02, 2021 Dec 02, 2021

This requires a script.

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 ,
Dec 02, 2021 Dec 02, 2021

There are several ways to do this.

Here's one script that will work. 

 

var nSalary = Number(this.getField("Salary").value);
event.value = Math.round(/100)*100;

You'll need to change the "Salary" field name to the name of this field on your form.

Also, the script assumes a valid number, or blank is entered into the "Salary" field. If this is not a valid assumption, then the script will need additional code to protect the calculation.

 

 

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
New Here ,
Dec 02, 2021 Dec 02, 2021

Thank you so much Thom. It comes back saying "SyntaxError: unterminated regular ex pression literal 1: at line 2

 

Im completely new at this so I would really appreciate any help. The salary field is called "Salary" and the field I am trying to pull that to and round up is called "TotalCoverage" if that helps?

 

 

 

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 ,
Dec 02, 2021 Dec 02, 2021

Use:

var nSalary = Number(this.getField("Salary").value); event.value = Math.round(nSalary/100)*100;

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 ,
Dec 02, 2021 Dec 02, 2021

Sorry about that. The script is missing a variable.  Got lost in editing 😞  Bernd has shown the corrected code.

 

var nSalary = Number(this.getField("Salary").value);
event.value = Math.round(nSalary/100)*100;

 

 

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
New Here ,
Dec 03, 2021 Dec 03, 2021

This is great thank you. 

 

One last question: If I wanted to take the Salary and round that up to the nearest thousand and then multiply it by 2.5 what would I do? I tried this on my own but its not rounding properly

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 ,
Dec 03, 2021 Dec 03, 2021

Use this:

 

event.value = (Math.round(nSalary/1000)*1000) * 2.5;

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
New Here ,
Dec 10, 2021 Dec 10, 2021
LATEST

thanks! this worked!

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