The toFixed() Method

The toFixed() method returns a string, with the number written with a specified number of decimals:

HTML file: Displayed by browser:
<body>
<p>The toFixed() method rounds a number to a given number of digits.</p>
<p id="demo"></p>
<script>
var x = 9.656;
document.getElementById("demo").innerHTML =
    x.toFixed(0) + "<br />" +    // returns 10
    x.toFixed(2) + "<br />" +    // returns 9.66
    x.toFixed(4) + "<br />" +    // returns 9.6560
    x.toFixed(6);                       // returns 9.656000
</script>
</body>

The toFixed() method rounds a number to a given number of digits.

This method is perfect for working with money.

BackTable of ContentsNext