Skip to main content
Participant
October 12, 2017
Question

Need help having the following formula round to the nearest hundredth

  • October 12, 2017
  • 1 reply
  • 693 views

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);
}

This topic has been closed for replies.

1 reply

Participating Frequently
October 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;
}

Inspiring
October 24, 2017

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.