Skip to main content
Participant
Feb 14, 2023 7:54 PM UTC
Answered

Need to reduce decimal places after averaging

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 14, 2023 9:17 PM UTC

Change the last line to:

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

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Feb 14, 2023 9:17 PM UTC

Change the last line to:

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

Translate
Participant
Feb 14, 2023 9:25 PM UTC

Much respects to you! 

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

Translate