Computer >> Computer tutorials >  >> Programming >> Javascript

Number.toPrecision() function in JavaScript


The toPrecision() function of the Number object accepts a value representing the precision and returns the representation of a (current) number to the specified precision.

Syntax

Its Syntax is as follows

num.toPrecision(num);

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var num = 3325.462;
      result = num.toPrecision(2);
      document.write("Result: " + result);
   </script>
</body>
</html>

Output

Result: 3.3e+3

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var num = 3325.462;
      result = num.toPrecision();
      document.write("Result: " + result);
   </script>
</body>
</html>

Output

Result: 3325.462