Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Math round() Method

Improve Article
Save Article
  • Last Updated : 30 Dec, 2022
Improve Article
Save Article

The Javascript Math.round() method is used to round a number to its nearest integer. If the fractional part of the number is greater than or equal to .5, the argument is rounded to the next higher integer. If the fractional part of the number is less than .5, the argument is rounded to the next lower integer.

Syntax:

Math.round(value);

Parameters: This method accepts a single parameter as mentioned above and described below

  • value: It is the number that you want to round off.

Return Value: The Math.round() method returns the value of the given number rounded to the nearest integer.

Below is an example of the Math round() Method.

Example: To round off a number to its nearest integer. 

javascript




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


Output:

Number after rounding : 6

 Program 1: The Math.round() method itself rounds off a negative number when passed as a parameter to it. To round off a negative number to its nearest integer, the Math.round() method should be implemented in the following way: 

javascript




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


Output:

Number after rounding : -6

Program 2: Below program shows the result of the Math.round() method when the parameter has “.5” in decimal. 

javascript




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


Output:

Number after rounding : -12
Number after rounding : 13

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!