JavaScript | Math.round( ) function

The Math.round() function in JavaScript is used to round the number passed as parameter to its nearest integer.
Syntax

Math.round(value)

    Parameters :

  • The number to be rounded to its nearest integer.
  • Returns :

  • Result after rounding the number passed as a parameter to the function passed as parameter.


  • Below are some examples to illustrate the Math.round() function:

    1. Rounding Off a number to its nearest integer: To round off a number to its nearest integer, the math.round() function should be implemented in the following way:



      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <script type="text/javascript">
          var round =Math.round(5.8);
          document.write("Number after rounding : " + round); 
      </script>

      chevron_right

      
      

      Output:

      Number after rounding : 6
    2. Rounding Off a negative number to its nearest integer: The Math.round() function itself rounds off a negative number when passed as parameter to it. To round off a negative number to its nearest integer, the Math.round() function should be implemented in the following way:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <script type="text/javascript">
          var round =Math.round(-5.8);
          document.write("Number after rounding : " + round); 
      </script>

      chevron_right

      
      

      Output:

      Number after rounding : -6
    3. Math.round() function, when parameter has “.5” as decimal: Below program shows the result of Math.round() function when the parameter has “.5” in decimal.

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <script type="text/javascript">
          var round =Math.round(-12.5);
          document.write("Number after rounding : " + round);
          var round =Math.round(12.51);
          document.write("Number after rounding : " + round); 
      </script>

      chevron_right

      
      

      Output:

      Number after rounding : -12
      Number after rounding : 13

    Errors and Exceptions
    1. A non-numeric string passed as parameter returns NaN
    2. An array with more than 1 integer passed as parameter returns NaN
    3. An empty variable passed as parameter returns NaN
    4. An empty string passed as parameter returns NaN
    5. An empty array passed as parameter returns NaN

    Below are some examples that illustrate the Math.floor() function in JavaScript:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- NEGATIVE NUMBER EXAMPLE -->
    <script type="text/javascript">
        document.write(Math.round(-2)); 
        document.write(Math.round(-2.56));          
    </script>

    chevron_right

    
    

    Output:

    -2
    -3
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- POSITIVE NUMBER EXAMPLE -->
    <script type="text/javascript">
        document.write(Math.round(2)); 
        document.write(Math.round(2.56));          
    </script>

    chevron_right

    
    

    Output:

    2
    3
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- STRING EXAMPLE -->
    <script type="text/javascript">
        document.write(Math.floor("Geeksforgeeks"));          
    </script>

    chevron_right

    
    

    Output:

    NaN
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!-- ADDITION INSIDE FUNCTION EXAMPLE -->
    <script type="text/javascript">
        document.write(Math.floor(7.2+9.3));           
    </script>

    chevron_right

    
    

    Output:

    17
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.