View Full Version : rounding numbers in javascript. Best way?
Steven_Smith
12-05-2002, 09:10 PM
What is the best way to round numbers in javascript?
I am trying to round a number to two decimal places.
using.
no_kilos=parseInt(no_kilos);
Thanks for any help.
Steve
Roelf
12-05-2002, 09:19 PM
http://www.javascriptkit.com/javatutors/round.shtml
kwhubby
12-06-2002, 12:01 AM
to round to the nearest whole number simply do a Math.round(variable to round )
so 2.2388 = 2
and 2.5 = 3
2.643 = 3
2.99999 = 3
Math.floor() will remove the value of the decimal eg... 2.2388 = 2
2.5 = 2
2.643 = 2
2.99999 = 2
Steven_Smith
12-06-2002, 12:15 AM
Thank you very much :thumbsup:
Something to remember:
2.34651.toFixed(2) == "2.35"
kwhubby
12-06-2002, 02:28 AM
cool!! :thumbsup: , i did not know that one! I would have just done
x = 3.52938759860
Math.floor(x*100)
x = x/100
and than x would be 3.52
chrismiceli
12-06-2002, 02:38 AM
isn't .toFixed() not fully supported yet, or not as compatible as the manual way?
5th-gen browsers or better.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.