JavaScript | Math.fround( ) Function

While performing operations on numbers we might sometimes want to use 32-bit numbers. JavaScript internally uses 64-bit floating point numbers and not 32-bit. So checking equality of 32-bit numbers with 64-bit numbers may fail. JavaScript provides us with a buitin function to cast a 64-bit floating point numbers to 32-bit. The Math.fround() function in JavaScript is used to find the nearest 32-bit single precision float representation of a given Number. Math.fround() function in JavaScript is also used to cast the 64-bit float to a 32-bit float.

Syntax:

Math.fround(value)

Parameters: This function accepts a single parameter value . This parameter represents the number for which we want to find the nearest 32-bit single precision float representation.



Return Value: The Math.fround() function returns the nearest 32-bit single precision float representation of the given number.

Below examples illustrates the Math.fround() function in JavaScript:

  1. When a positive number is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.fround(3));          
    </script>

    chevron_right

    
    

    Output:

    3
  2. When a positive number with decimal places is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.fround(3.345));          
    </script>

    chevron_right

    
    

    Output:

    3.3450000286102295
  3. When a negative number is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.fround(-3));          
    </script>

    chevron_right

    
    

    Output:

    -3
  4. When a negative number with decimal places is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.fround(-3.345));          
    </script>

    chevron_right

    
    

    Output:

    -3.3450000286102295
  5. When zero is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.fround(0));          
    </script>

    chevron_right

    
    

    Output:

    0


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 :
Practice Tags :


Be the First to upvote.


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