JavaScript toPrecision() function is used to format a number to a specified length. This method returns a string representing the Number object to the specified precision.
number.toPrecision(x)
Number object to specified precision with JavaScript toPrecision()
Simple example code format a number to a specified length.
<!DOCTYPE html>
<html>
<head>
<script>
let num = 13.3714;
let n = num.toPrecision(4);
console.log(n);
</script>
</head>
<body>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this JS precision function.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version