Math.round() JavaScript

Math.round JavaScript | In JavaScript, the Math.round() function rounds the number supplied as an argument to the integer value. The parameter is rounded to the digits with the next highest absolute value if the fractional component is bigger than 0.5.

If it’s less than 0.5, the parameter is rounded to the lowest absolute value integer. The parameter is rounded to the next number in the heading of + if the fractional component is exactly 0.5. Remember that this varies from many languages’ round() methods, which commonly round negative values with a fractional portion of exactly 0.5 to the next integer away from zero, rather than returning a different result.


The integer to which the number should be rounded.

The result of rounding the number that was supplied as an input to the function.

Math.round JavaScript Examples

The Math.round() function of JavaScript is demonstrated in the following examples:-

1) How to round a number to the nearest integer: JavaScript math.round method can be used to round a number to the closest integer in the following way:




We used the Math class to call the round() function in this example.

To demonstrate what the Math.round JavaScript method delivers, we have written the result of the round() method to the console.

2) Rounding a negative number to the closest integer is as follows: When supplied a negative value as an argument, the Math.round JavaScript function rounds it off. The Math.round() function should be used to round off a negative number to the nearest integer as follows:-




The console log output was -9, which is -9.1 rounded up to the nearest digit. When dealing with negative values, the Math.round JavaScript function rounds up towards zero, returning -9 rather than -10 in this case. This behavior differs from that of other languages’ round functions.

3) When the parameter is “.5” as a decimal, use the Math.round() function: The following software displays the outcome of the Math.round JavaScript method when the parameter has a decimal value of “.5”.




However, there are some errors and exceptions that you will have to deal with when it comes to using the Math.round function. They are as follows:-

  1. A parameter with a non-numeric string returns NaN.
  2. When more than one integer is supplied as a parameter, the array returns NaN.
  3. An empty array, variable, or string supplied as a parameter yields NaN.

This brings us to the end of this article, where we saw how to use the Math.round JavaScript function and its related errors. Also see:- How to Round to Nearest Hundredth JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *