AppDividend
Latest Code Tutorials

Javascript Math Round Example | Math.round() Tutorial

0

Javascript Math Round Example | Math.round() Tutorial is today’s topic. The Math.round() function returns the value of a number rounded to the nearest integer. If the fractional portion of an argument is more than 0.5, the argument is turned to an integer with the next higher absolute value. If it is less than 0.5, then the argument is rounded to the integer with the lower value.  If the fractional portion is exactly 0.5, then the argument is rounded to the next integer in the direction of +∞.

Javascript Math Round Example

The syntax of Math.round() is following.

Math.round(x)

Where is a number. The value of a given number rounded to the nearest integer.

Let’s see the following example.

// app.js

console.log(Math.round(21.19));
console.log(Math.round(19.21));
console.log(Math.round(46.21));
console.log(Math.round(21.52));

The output is following.

Javascript Math Round Example | Math.round() Tutorial

The Math.round() function returns a value of the number rounded to the nearest integer.

If we want to round off a number to its nearest integer, the Math.round() function should be implemented.

The Math.round() function itself rounds off a negative number when passed as the parameter to it. See the following example.

// app.js

console.log(Math.round(-21.52));

The output is following.

Math.round() Tutorial

Finally, Javascript Math Round Example | Math.round() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.