What is the use of the Math.round() method in JavaScript?

The Math.round() method in JavaScript is used to round a number to the nearest integer.

The method takes a single argument, which is the number to be rounded. If the fractional part of the number is 0.5 or greater, the method rounds the number up to the next integer. If the fractional part is less than 0.5, the method rounds the number down to the previous integer.

Here is an example of using the Math.round() method:

var num1 = 3.14159;
var num2 = 7.5;
var num3 = -1.234;

console.log(Math.round(num1)); // Outputs 3
console.log(Math.round(num2)); // Outputs 8
console.log(Math.round(num3)); // Outputs -1

In the first example, the Math.round() method rounds the number 3.14159 down to 3.

In the second example, the method rounds the number 7.5 up to 8.

In the third example, the method rounds the negative number -1.234 down to -1.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: