Exemples de code

Formater un nombre à virgule flottante

La méthode toFixed() permet de formater un nombre à virgule flottante.

let nombre = 123.456789;
document.write(nombre);
// 123.456789

document.write(nombre.toFixed(3));
// 123.457 <-- arrondi

document.write(nombre.toFixed(10));
// 123.4567890000 <-- ajoute des zéros

On remarque au deuxième exemple, que la méthode effectue un arrondi.