Format a number to a specified length: - Javascript Number

Javascript examples for Number:toPrecision

Description

Format a number to a specified length:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from   w  ww. j a  v  a2  s. co  m*/
    var num = 13.3714;
    var a = num.toPrecision();
    var b = num.toPrecision(2);
    var c = num.toPrecision(3);
    var d = num.toPrecision(10);

    var n = a + "<br>" + b + "<br>" + c + "<br>" + d;

    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

Related Tutorials