La référence javascript : Objets, méthodes, propriétés

Méthode : Math.round()
Retourne l'arrondi (entier) du réel passé en paramètre
Compatible :

Syntaxe
Integer Math.round(Float x)

Description
Retourne l'arrondi (entier) du réel passé en paramètre

Exemple Les règles d'arrondis !
Code source
<SCRIPT language=javascript>
document.write('round(4.52) = '+Math.round(4.52)+"<BR>");
document.write('round(4.12) = '+Math.round(4.12)+"<BR>");
document.write('round(-4.52) = '+Math.round(-4.52)+"<BR>");
document.write('round(1.5) = '+Math.round(1.5)+"<BR>");
var test = 1/3;
document.write('round(1/3) à 2 décimales = '+Math.round(test*100)/100+"<BR>");
</SCRIPT>


Résultat


Explication
La méthode round() arrondit à l'entier le plus proche, selon les règles d'arrondi commercial.

Pour avoir un arrondi deux chiffres après la virgule, il faut utiliser Math.round(variable*100)/100




Reproduction ou diffusion interdite sans autorisation de Tout JavaScript.com