// JavaScript uses the 64-bit IEEE-754 floating point standard for storing numbers
const num = 314.15926535;
// toFixed(n) will convert a float to a string with n digits after the decimal point
num.toFixed() // "314" (same as num.toFixed(0))
num.toFixed(2) // "314.16"
num.toFixed(6) // "314.159265"
// toPrecision(n) will convert a float to a string with n digits total
num.toPrecision() // "314.15926535" (same as input)
num.toPrecision(2) // "3.1e+2" (sometimes will be in scientific notation)
num.toPrecision(6) // "314.159"
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The toPrecision() Method</h2>
<p>toPrecision() formats a number to a specified length:</p>
<p id="demo"></p>
<script>
let num = 0.001658853;
document.getElementById("demo").innerHTML =
num.toPrecision(2) + "<br>" +
num.toPrecision(3) + "<br>" +
num.toPrecision(10);
</script>
</body>
</html>