Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Math.round( ) function

Improve Article
Save Article
  • Last Updated : 29 Nov, 2022
Improve Article
Save Article

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

Syntax:

Math.round(value)

Parameters:

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

Example 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: 

Javascript




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

Output:

Number after rounding : 6

Example 2: Rounding Off a negative number to its nearest integer

The Math.round() function 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() function 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

Example 3: Math.round() function, when a parameter has “.5” as a decimal

Below program shows the result of Math.round() function when the parameter has “.5” as a decimal. 

Javascript




<script type="text/javascript">
    var round =Math.round(-12.5);
    console.log("Number after rounding : " + round);
    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:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!