views:

287

answers:

2

I have a variable i=28.57142857142857; I want to alert(i); alert this variable on user screen. But I want only two digits after decimal. i.e 28.57

How to do it.

+4  A: 

try using toFixed:

 alert(i.toFixed(2));

If you need the precision mentioned in the next answer from Jappie, you could overwrite the native toFixed method like this:

Number.prototype.toFixed = function (precision) {
 var power = Math.pow(10, precision || 0);
 return String(Math.round(this * power) / power);
};
KooiInc
+1  A: 

How about

alert(Math.round(i * 100) / 100);

There are problems with toFixed. See this post.

Jappie