Number toPrecision()

The toPrecision() method returns either the fixed or the exponential representation of a number. This method takes the total number of digits to represent the number as the argument.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var num = 99; 
        document.writeln(num.toPrecision(1)); //"1e+2" 
        document.writeln(num.toPrecision(2)); //"99" 
        document.writeln(num.toPrecision(3)); //"99.0" 
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Number:
  1. The Number Type
  2. Number toExponential()
  3. Number toFixed()
  4. Number toPrecision()
  5. Number toString()
  6. Number valueOf()
  7. Converting Strings to Numbers
  8. typeof and instanceof for Number objects and primitive values