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

Need help having the following formula round to the nearest hundredth

New Here ,
Oct 12, 2017 Oct 12, 2017

I have the following formula and it works but I would like my answer to round to the nearest hundredth.    Any help would be greatly appreciated.

var ST = this.getField("ST").value;
var SR = this.getField("SR").value;
var SS = this.getField("SS").value;
var SA = this.getField("SA").value;
var WT = this.getField("WT").value;
var WR = this.getField("WR").value;
var WS = this.getField("WS").value;
var WA = this.getField("WA").value;

if((SA == "" && SA != "0")&&(WA != "0"))
{
    event.value = "TBD";
}
else
{
    event.value =     (ST*WT)+(SR*WR)+(SS*WS)+(SA*WA);
}

TOPICS
Acrobat SDK and JavaScript , Windows
634
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
New Here ,
Oct 23, 2017 Oct 23, 2017

Try:

var ST = this.getField("ST").value*100;
var SR = this.getField("SR").value*100;
var SS = this.getField("SS").value*100;
var SA = this.getField("SA").value*100;
var WT = this.getField("WT").value*100;
var WR = this.getField("WR").value*100;
var WS = this.getField("WS").value*100;
var WA = this.getField("WA").value*10;

if((SA == "" && SA != "0")&&(WA != "0"))
{
    event.value = "TBD";
}
else
{
    event.value =     Math.round((ST*WT)+(SR*WR)+(SS*WS)+(SA*WA))/100;
}

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
LEGEND ,
Oct 24, 2017 Oct 24, 2017
LATEST

Math.round is for rounding integers and does not work for rounding all floating point numbers. So if you want to round the result of the calculation you would need to multiply the result by 100 ,then apply the Math.round and then divide that result by 100.

How is your result field formatted?

It certainly is not "Number". If you want the displayed result to only display to to 2 digits rounded, the set the number of decimal places to 2.

If you want the value of the calculation set to 2 decimal places I would look at using the util.printf() method.

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