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
Otherwise, you can use the Number class and do
There you go for everyone still looking ^^
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 ^^
You can do to check if it is supported.
Here’s more info: https://pageresource.com