Does anyone know if javascript have a math function that will round a floating decimal number to the tenth, hundredth...etc., position? The Math.Round() rounds it to an integer. I don't want an integer.
for some reason the ^ operator isn't always an Math operator... it's also a bitwise exclusive or operator. this is what i use.
Number.prototype.toDec = function(p)
{
var x = Math.pow(10, p);
var y = Math.round(this * x)/x;
return y;
}
you use it like this:
var y = 100.7896;
y = y.toDec(2);
alert;
y will be 100.79
the above function won't round to decimals place if it doesn't have enough digits... so, i use this function... it returns a string, but, it's the only way to format a number with extra 0's on the end.
Number.prototype.toDec = function(e)
{
var x, y, boo, bo;
x = Math.pow(10,e);
y = new String(Math.round(this * x)/x);
boo = (y.indexOf("." > -1)?(y.split("."[1].length == e)?false:true:true;
if(boo)
{
bo = (y.indexOf("." > -1)?false:true;
if(bo){y+=".";for(var i = 0; i < e; i++){y+="0";}}
else
{
boo = e - y.split("."[1].length;
for(var i = 0; i < boo; i++){y+="0";}
}
}
return y;
} luciddream@subdimension.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.