Javascript Arrondi à 2 chiffres après la virgule

Solved
jeremy -  
 gravityaerox -
Bonjour,

je cherche une fonction javascript qui permettrait d'arrondir un nombre à deux chiffres après la virgule (un peu comme number_format du php), car la fonction round() ne permet d'arrondir qu'à l'unité!

Merci
Ciao

Jérémy
Configuration: Windows Vista
Firefox 2.0.0.14

3 answers

Korri
 
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 ^^
94
Korri
 
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 do
if (your_number.toFixed)
to check if it is supported.

Here’s more info: https://pageresource.com
0
égaré
 
Thank you, I was just looking for that.
0