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

Rounding to 2 decimal places ONLY

Community Beginner ,
Mar 28, 2023 Mar 28, 2023

I have a form in which I am using the below custom calculation script:

 

// Acquire Inputs
var nA = Number(this.getField("Yes1").valueAsString);
var nB = Number(this.getField("TotalOSAudited").valueAsString);
var nSum = Number(this.getField("YesPerc1").valueAsString);

 

// Perform Calculation
var nSum = (nA / nB)*100;

 

// Assign Result to Field
event.value = nSum;

 

The select format category is None.  I need the calculation to round to 2 decimal places ONLY.  I have spent the last two days going cross eyed searching for the answer.  I am a newby to any sort of script.  Thanks for the help!

TOPICS
JavaScript , PDF forms
451
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 ,
Mar 28, 2023 Mar 28, 2023
LATEST

You also need to check that nB is not 0, or you will get NaN error because of trying to divide with zero.

To make two decimals, use like this:

event.value = nSum.toFixed(2);

 

Why do you declare var nSum to field "YesPerc1" and then also declare it again in calculation?

 

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