JavaScript Number toPrecision( ) Method - GeeksforGeeks

22 Dec, 2021
Below is the example of the Number toPrecision( ) Method.
Example:        <script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(3));          </script>
        <script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(3));          </script>
Output:213
213
The toPrecision() method in Javascript is used to format a number to a specific precision or length. If the formatted number requires more digits than the original number then decimals and nulls are also added to create the specified length.
Syntax:
number.toPrecision(value)
The toPrecision() method is used with a number as shown in above syntax using the ‘.’ operator. This method will format a number to a specified length.
Parameters: This method accepts a single parameter value. This parameter is also optional and it represents the value of the number of significant digits the user wants in the formatted number.
Return Value: The toPrecision() method in JavaScript returns a string in which the number is formatted to the specified precision.
Below are some examples to illustrates toPrecision() method:
Passing no arguments in the toPrecision() method: If no arguments is passed to the toPrecision() method then the formatted number will be exactly the same as input number. Though, it will be represented as a string rather than a number.Code# 1:        <script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision());          </script>Output:213.45689Passing an argument in the toPrecision() method: If the length of precision passed to the toPrecision() method is smaller than the original number then the number is rounded off to that precision.Code# 2:<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(4));          </script>Output:213.5Passing an argument which results in addition of null in the output: If the length of precision passed to the toPrecision() method is greater than the original number then zero’s are appended to the input number to meet the specified precision.Code# 3:<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(12));        var num2 = 123;    document.write(num2.toPrecision(5));        </script>Output:213.456890000
123.00

Passing no arguments in the toPrecision() method: If no arguments is passed to the toPrecision() method then the formatted number will be exactly the same as input number. Though, it will be represented as a string rather than a number.Code# 1:        <script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision());          </script>Output:213.45689
        <script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision());          </script>
Output:
213.45689
Passing an argument in the toPrecision() method: If the length of precision passed to the toPrecision() method is smaller than the original number then the number is rounded off to that precision.Code# 2:<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(4));          </script>Output:213.5
<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(4));          </script>
Output:
213.5
Passing an argument which results in addition of null in the output: If the length of precision passed to the toPrecision() method is greater than the original number then zero’s are appended to the input number to meet the specified precision.Code# 3:<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(12));        var num2 = 123;    document.write(num2.toPrecision(5));        </script>Output:213.456890000
123.00

<script type="text/javascript">    var num=213.45689;    document.write(num.toPrecision(12));        var num2 = 123;    document.write(num2.toPrecision(5));        </script>
Output:
213.456890000
123.00

Note: If the precision specified is not in between 1 and 100 (inclusive), it results in a RangeError.
Supported Browsers:
Google Chrome 1 and above
Internet Explorer 5.5 and above
Firefox 1 and above
Apple Safari 2 and above
Opera 7 and above
shubham_singh
ysachin2314
JavaScript-Methods
JavaScript-Numbers
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Set the value of an input field in JavaScript
How to read a local text file using JavaScript?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?