Skip to content

JavaScript Math round Method

  • by

JavaScript Math round Method is used to round a given number to the nearest integer. It takes a single argument, which is the number to be rounded.


To round a number to a specific number of decimal places, you can use the following syntax:

Math.round(num * 10**decimalPlaces) / 10**decimalPlaces

JavaScript Math round Method example

Simple example code.

<!DOCTYPE html>
        let num = 2.5;
        let rounded = Math.round(num);



JavaScript Math round Method

Using Math.round() method to round to a specified number of decimal places. You can multiply the number by a factor of 10 raise to the power of the number of decimal places you want and divide the result by the same factor.

let num = 2.546;
let rounded = Math.round(num * 100) / 100;

console.log(rounded); // Output: 2.55

The Math.round() method returns the rounded value of the input number as an integer.

Math.round() behaviorExampleOutput
If the fractional portion > 0.5Math.round(2.6)3
If the fractional portion < 0.5Math.round(2.4)2
If the fractional portion = 0.5Math.round(2.5)3

Do comment if you have any doubts or suggestions on this Js math topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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