Highlighted

## Round to nearest 10 in fillable pdf

Community Beginner ,
Jul 08, 2020

Copied

I have already created code that works to average 2 or 3 numbers and I need that result to be rounded either up or down to the nearest 10.  I've tried Math.round() with no result.  Here is my code:

//determine average of 2-3 values
event.value=0
var nStress=(this.getField("StressPSIRow1").value);
var nStress1=(this.getField("StressPSIRow2").value);
var nStress2=(this.getField("StressPSIRow3").value);
var nCnt = 0;
for(var i=1; i<4;i++){var nVal=this.getField("StressPSIRow"+i).value;
if((nVal !=0))nCnt++;}
if(nStress>0||nStress1>0||nStress2>0)event.value=((nStress+nStress1+nStress2)/nCnt);

This is the first time I've done this so everything I've learned has been from these types of forums.  I would appreciate if anyone can help me figure out where to put either the Math.round into this code or something else that will work.

Thanks!

So, you're code is a bit repetitive. You are using two different techniques, and niether is entirely correct.

But your on the right track. The best way to do this is to use the loop to preform the sum.   And keep the count for excluding zero/blank values. So it's important to do an explicit number conversion of the field value.

``````var nSum = 0, nCnt=0;
for(var i=1; i<4;i++)
{
var nVal= Number(this.getField("StressPSIRow"+i).value);
if(nVal !=0)
{
nSum += nVal;
nCnt++;
}
}
// Average and Round to nearest 1/10^n
event.value = (nSum/nCnt).toFixed(-1);``````

Views

40

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more ## Round to nearest 10 in fillable pdf

Community Beginner ,
Jul 08, 2020

Copied

I have already created code that works to average 2 or 3 numbers and I need that result to be rounded either up or down to the nearest 10.  I've tried Math.round() with no result.  Here is my code:

//determine average of 2-3 values
event.value=0
var nStress=(this.getField("StressPSIRow1").value);
var nStress1=(this.getField("StressPSIRow2").value);
var nStress2=(this.getField("StressPSIRow3").value);
var nCnt = 0;
for(var i=1; i<4;i++){var nVal=this.getField("StressPSIRow"+i).value;
if((nVal !=0))nCnt++;}
if(nStress>0||nStress1>0||nStress2>0)event.value=((nStress+nStress1+nStress2)/nCnt);

This is the first time I've done this so everything I've learned has been from these types of forums.  I would appreciate if anyone can help me figure out where to put either the Math.round into this code or something else that will work.

Thanks!

So, you're code is a bit repetitive. You are using two different techniques, and niether is entirely correct.

But your on the right track. The best way to do this is to use the loop to preform the sum.   And keep the count for excluding zero/blank values. So it's important to do an explicit number conversion of the field value.

``````var nSum = 0, nCnt=0;
for(var i=1; i<4;i++)
{
var nVal= Number(this.getField("StressPSIRow"+i).value);
if(nVal !=0)
{
nSum += nVal;
nCnt++;
}
}
// Average and Round to nearest 1/10^n
event.value = (nSum/nCnt).toFixed(-1);``````

Views

41

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Jul 08, 2020

Copied

I moved your post over from the poorly named Community Help forum, which is for getting help using this forum system, to a better forum, Acrobat forum.

I hope this helps. Best of luck to you.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Community Beginner ,
Jul 08, 2020

Copied

Thank you!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Jul 08, 2020

Copied

So, you're code is a bit repetitive. You are using two different techniques, and niether is entirely correct.

But your on the right track. The best way to do this is to use the loop to preform the sum.   And keep the count for excluding zero/blank values. So it's important to do an explicit number conversion of the field value.

``````var nSum = 0, nCnt=0;
for(var i=1; i<4;i++)
{
var nVal= Number(this.getField("StressPSIRow"+i).value);
if(nVal !=0)
{
nSum += nVal;
nCnt++;
}
}
// Average and Round to nearest 1/10^n
event.value = (nSum/nCnt).toFixed(-1);``````

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more LATEST
Community Beginner ,
Jul 08, 2020

Copied

Looks like that works.  Thanks!!!  I'm definitely not a programmer.  Sort of thrown into that pit for this document.  Learned a lot from this forum.  Appreciate all your help.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more Resources One Stop Solution for Acrobat
Edit a PDF