2 Replies Latest reply on Oct 24, 2017 6:50 AM by gkaiseril

Need help having the following formula round to the nearest hundredth

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

• 1. Re: Need help having the following formula round to the nearest hundredth

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

• 2. Re: Need help having the following formula round to the nearest hundredth

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.