Calculation needs to round up, not down

Mickey_Cegon
Tera Expert

I am in need of help to script a calculation where the answer is rounded up, not down. Apparently, in the system we're replacing w/ServiceNow, it was able to add a math statement "round", which would force the calculation to round up whatever is in the calculation.

Previous program has this:

If you are using the (=round)   function it would round up.

=ROUND(5181*0.055,2)

Answer:                          
284.96      

My script is:

var per = u_personal_miles; //this is 5181 in above calc, and is an integer field

var gas = (per *'.055');//comes out to 284.955
gas = gas.toFixed(2);//comes out to 284.95

Any advice would be welcome.

Mickey Cegon

Farm Bureau Financial Services, Inc.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Here's a simple workaround that appears to work:



var per = 5181; //this is 5181 in above calc, and is an integer field


var gas = (per * .055);//comes out to 284.955


gas = Math.round(gas * 100) / 100; // 284.96


View solution in original post

6 REPLIES 6

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Mickey,



I would take a look at this stackoverflow post that describes why you're seeing the issue and some advice on resolving it:



Javascript toFixed Not Rounding - Stack Overflow


Thanks, Brad! I bookmarked that link, for future reference.



Mickey


Chuck Tomasi
Tera Patron

Here's a simple workaround that appears to work:



var per = 5181; //this is 5181 in above calc, and is an integer field


var gas = (per * .055);//comes out to 284.955


gas = Math.round(gas * 100) / 100; // 284.96


Hey Chuck, there are actually a few situations where that doesn't work in some browsers if this is client side described in the post. If it's server side I think it's fine though.