JavaScript | toPrecision( ) Function

The toPrecision() method in Javascript is used to format a number to a specific precision or length. If the formatted number requires more number of digits than original number then decimals and nulls are also added to create the specified length.

Syntax:

number.toPrecision(value)

The toPrecision() function is used with a number as shown in above syntax using the ‘.’ operator. This function will format a number to a specified length.

Parameters: This function 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() function:

  1. Passing no arguments in the toPrecision() method: If no arguments is passed to the toPrecision() function then the formatted number will be exactly the same as input number. Though, it will be represented as a string rather than a number.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num=213.45689;
        document.write(num.toPrecision());          
    </script>

    chevron_right

    
    

    Output:

    213.45689
  2. Passing an argument in the toPrecision() method: If the length of precision passed to the toPrecision() function is smalller than the original number then the number is rounded off to that precision.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var num=213.45689;
        document.write(num.toPrecision(4));          
    </script>

    chevron_right

    
    

    Output:

    213.5
  3. Passing an argument which results in addition of null in the output: If the length of precision passed to the toPrecision() function is greater than the original number then zero’s are appended to the input number to meet the specified precision.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    213.456890000
    123.00
    

Note: If the precision specified is not in between 1 and 100 (inclusive), it results in a RangeError.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.