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.
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);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With