The toPrecision() Method

The toPrecision() method returns a string, with a number written with a specified length:

HTML file: Displayed by browser:
<body>
<p>The toPrecision() method returns a string, with a number written with a specified length.</p>
<p id="demo"></p>
<script>
var x = 9.656;
document.getElementById("demo").innerHTML =
    x.toPrecision() + "<br />" +      // returns 9.656
    x.toPrecision(2) + "<br />" +    // returns 9.7
    x.toPrecision(4) + "<br />" +    // returns 9.656
    x.toPrecision(6);                       // returns 9.65600
</script>
</body>

The toPrecision() method returns a string, with a number written with a specified length.


BackTable of ContentsNext