Below is the example of the Math round() Method.
- Example: To round off a number to its nearest integer.
<script type="text/javascript">varround =Math.round(5.8);document.write("Number after rounding : "+ round);</script>chevron_rightfilter_none - Output:
Number after rounding : 6
The 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 which you want to round off.
Return Value: The Math.round() method returns the value of the given number rounded to the nearest integer.
More codes for the above method are as follows:
Program 1: The Math.round() method 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() method should be implemented in the following way:
<script type="text/javascript"> var round =Math.round(-5.8); document.write("Number after rounding : " + round); </script> |
Output:
Number after rounding : -6
Program 2: Below program shows the result of Math.round() method when the parameter has “.5” in decimal.
<script type="text/javascript"> var round =Math.round(-12.5); document.write("Number after rounding : " + round + "<br>"); var round =Math.round(12.51); document.write("Number after rounding : " + round); </script> |
Output:
Number after rounding : -12 Number after rounding : 13
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- JavaScript | Math.round( ) function
- PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
- Round off a number to the next multiple of 5 using JavaScript
- How to Round off Time to Nearest 5 Min using JavaScript ?
- How to Round Time to the Nearest Quarter Hour using JavaScript ?
- JavaScript Math cosh() Method
- JavaScript Math random() Method
- JavaScript Math abs( ) Method
- JavaScript Math sqrt( ) Method
- JavaScript Math floor() Method
- JavaScript Math pow( ) Method
- JavaScript Math trunc( ) Method
- JavaScript Math exp( ) Method
- JavaScript Math log( ) Method
- JavaScript Math cos( ) Method
- JavaScript Math tan( ) Method
- JavaScript Math ceil( ) Method
- JavaScript Math sin( ) Method
- JavaScript Math min( ) Method
- JavaScript Math max() Method
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.


