Javascript Round to 2 Decimal Places
Solved
jeremy
-
gravityaerox -
gravityaerox -
Hello,
I'm looking for a JavaScript function that would round a number to two decimal places (a bit like number_format in PHP), as the round() function only allows rounding to the unit!
Thanks
Ciao
JérémyConfiguration: Windows Vista
Firefox 2.0.0.14
I'm looking for a JavaScript function that would round a number to two decimal places (a bit like number_format in PHP), as the round() function only allows rounding to the unit!
Thanks
Ciao
JérémyConfiguration: Windows Vista
Firefox 2.0.0.14
3 answers
-
The response is late, but it may help some:
There is no one-size-fits-all method for this, so you have to do
Math.round(your_number*100)/100;
Otherwise, you can use the Number class and do
var your_number = 12.3455633; // Your number variable your_number.toFixed(2); //will return 12.35..
There you go for everyone still looking ^^-
It's me again, just to say that this method (Number.toFixed()) is JavaScript 1.5 and it only works for IE5.5+ and NS6+
You can doif (your_number.toFixed)
to check if it is supported.
Here’s more info: https://pageresource.com -
-