PDA

View Full Version : Math.round or Math.floor



kornkid8281
November 13th, 2002, 06:10 PM
i want to round a decimal to like 1.5. here's what i have:
<hr>Math.floor((smallNum/bigNum)*100)<hr>

is there something i can do to this so that it'll round to like 11.5?

sbeener
November 13th, 2002, 08:30 PM
here's a function (which i put in the Math object, but could go anywhere) which rounds "n" number to "d" decimal.


Math.roundTo = function(n,d){
return Math.round(n/d) * d;
}

so if num = 1294.2258

Math.roundTo(num,.1) would return 1294.2
Math.roundTo(num,10) returns 1290

and so on

kornkid8281
November 13th, 2002, 09:56 PM
thank you so much! that was the perfect solution!