Skip to main content
Known Participant
March 28, 2023
Question

Rounding to 2 decimal places ONLY

  • March 28, 2023
  • 1 reply
  • 526 views

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!

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
March 29, 2023

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?