Tutorials Point


  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Javascript Number - toPrecision()


previous next AddThis Social Bookmark Button


Advertisements

Description:

This method returns a string representing the number object to the specified precision.

Syntax:

number.toPrecision( [ precision ] )

Here is the detail of parameters:

  • precision: An integer specifying the number of significant digits.

Return Value:

Returns a string representing a Number object in fixed-point or exponential notation rounded to precision significant digits.

Example:

<html>
<head>
<title>JavaScript toPrecision() Method </title>
</head>
<body>
<script type="text/javascript">
   var num = new Number(7.123456);
   document.write("num.toPrecision() is " + num.toPrecision());
   document.write("<br />"); 

   document.write("num.toPrecision(4) is " + num.toPrecision(4)); 
   document.write("<br />"); 

   document.write("num.toPrecision(2) is " + num.toPrecision(2)); 
   document.write("<br />"); 

   document.write("num.toPrecision(1) is " + num.toPrecision(1)); 
</script>
</body>
</html>

This will produce following result:

num.toPrecision() is 7.123456
num.toPrecision(4) is 7.123
num.toPrecision(2) is 7.1
num.toPrecision(1) is 7 

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements