Skip to main content
Participant
February 14, 2023
Answered

Need to reduce decimal places after averaging

  • February 14, 2023
  • 1 reply
  • 605 views

Hello, I am looking to reduce the number of decimal places after averaging. 

The average that I calculated 3+1+1 is 1.666666666666667

and it is displaying that answer, but what I would like is that after adobe calculates the average, it will only give me two decimal places. 1.67

 

This is the code that I am using: 

 

var nSum = 0; nCnt = 0;

for(var i=1;i<=10;i++)

{

var nVal = this.getField("Proskills" + i).value;

if(!/^\s*$/.test(nVal) && !isNaN(nVal))

{

nCnt++;

nSum += nVal;

{

}
event.value = (nCnt > 0)?nSum/nCnt:"N/A";

 

After I input 

then event.value =(util.printf("%0.2f",nVal));

 

I am no longer able to change the average 

 

Can you please help me calculate an overall average and them reduce my results to two decimal places

 

This topic has been closed for replies.
Correct answer try67

Change the last line to:

event.value = (nCnt > 0)?(nSum/nCnt).toFixed(2):"N/A";

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 14, 2023

Change the last line to:

event.value = (nCnt > 0)?(nSum/nCnt).toFixed(2):"N/A";

Participant
February 14, 2023

Much respects to you! 

thank you, I am pretty new at this and its my first ever attempt at Javascript