JavaScript | toFixed( ) Function

The toFixed() function in JavaScript is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal.

Syntax:

number.toFixed( value )

The toFixed() function is used with a number as shown in above syntax using the ‘.’ operator. This function will format a number using fixed-point notation.

Parameters: This function accepts a single parameter value . It signifies the number of digits to appear after the decimal point.

Return Value: It returns a number in the string representation. The number has the exact number of digits after the decimal place as mentioned in the toFixed() function.

Example

  • Using toFixed() function without any parameter: If there is no parameter specified in the toFixed() function then it doesn’t display any digits after the decimal place.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var test=213.73145;
        document.write(test.toFixed());          
    </script>

    chevron_right

    
    

    Output:

    214
  • Using toFixed() function with a parameter: If there is a parameter specified in the toFixed() function will return a number represented as string which will have exactly that number of digits after the decimal place.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var test=213.73145;
        document.write(test.toFixed(3));          
    </script>

    chevron_right

    
    

    Output:

    214.731
  • Using toFixed() function where the number is in exponential form: The toFixed() function can be used to convert an exponential number into a string representation with a specific number of digits after the decimal place.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var test=2.13e+15;
        document.write(test.toFixed(2));          
    </script>

    chevron_right

    
    

    Output:

    2130000000000000.00


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.