JavaScript | Math.round( ) function

09 Feb, 2022
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:
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:<script type="text/javascript">    var round =Math.round(5.8);    document.write("Number after rounding : " + round); </script>Output:Number after rounding : 6Rounding 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:<script type="text/javascript">    var round =Math.round(-5.8);    document.write("Number after rounding : " + round); </script>Output:Number after rounding : -6Math.round() function, when parameter has “.5” as decimal: Below program shows the result of Math.round() function when the parameter has “.5” in decimal.<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>Output:Number after rounding : -12
Number after rounding : 13
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:<script type="text/javascript">    var round =Math.round(5.8);    document.write("Number after rounding : " + round); </script>Output:Number after rounding : 6
<script type="text/javascript">    var round =Math.round(5.8);    document.write("Number after rounding : " + round); </script>
Output:
Number after rounding : 6
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:<script type="text/javascript">    var round =Math.round(-5.8);    document.write("Number after rounding : " + round); </script>Output:Number after rounding : -6
<script type="text/javascript">    var round =Math.round(-5.8);    document.write("Number after rounding : " + round); </script>
Output:
Number after rounding : -6
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.<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>Output:Number after rounding : -12
Number after rounding : 13
<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>
Output:
Number after rounding : -12
Number after rounding : 13
Errors and Exceptions1. A non-numeric string passed as parameter returns NaN2. An array with more than 1 integer passed as parameter returns NaN3. An empty variable passed as parameter returns NaN4. An empty string passed as parameter returns NaN5. An empty array passed as parameter returns NaN
Below are some examples that illustrate the Math.floor() function in JavaScript:
<!-- NEGATIVE NUMBER EXAMPLE --><script type="text/javascript">    document.write(Math.round(-2));     document.write(Math.round(-2.56));          </script>
Output:
-2
-3

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

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

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

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
ysachin2314
javascript-math
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.