How to Use the Math.round() Function in JavaScript

The Math.round() function in JavaScript is a built-in method that rounds a number to the nearest integer. If the fractional part of the number is 0.5 or higher, the number is rounded up to the next integer. If it is less than 0.5, the number is rounded down to the previous integer.

Syntax

The syntax for using Math.round() is as follows:

Math.round(number);

Where number is the value you want to round.

Examples

Example 1: Basic Usage

Let’s look at some basic examples of how Math.round() works.

console.log(Math.round(2.3)); // Output: 2
console.log(Math.round(2.6)); // Output: 3
console.log(Math.round(-2.3)); // Output: -2
console.log(Math.round(-2.6)); // Output: -3

In the first example, 2.3 is rounded down to 2 because the fractional part is less than 0.5. In the second example, 2.6 is rounded up to 3 because the fractional part is more than 0.5. The same logic applies to negative numbers.

Example 2: Rounding to a Specific Decimal Place

If you want to round a number to a specific decimal place, you can use a combination of multiplication and division.

function roundToDecimal(number, decimalPlaces) {
  const factor = Math.pow(10, decimalPlaces);
  return Math.round(number * factor) / factor;
}

console.log(roundToDecimal(3.14159, 2)); // Output: 3.14
console.log(roundToDecimal(2.71828, 3)); // Output: 2.718

In this example, the roundToDecimal function takes a number and the number of decimal places you want to round it to. It multiplies the number by 10^decimalPlaces, rounds it, and then divides by the same factor to get the result.

Example 3: Rounding Arrays

If you have an array of numbers that you want to round, you can use the map() method in combination with Math.round().

const numbers = [1.2, 2.5, 3.7, 4.1];
const roundedNumbers = numbers.map(num => Math.round(num));

console.log(roundedNumbers); // Output: [1, 3, 4, 4]

In this example, the map() method is used to apply Math.round() to each element of the array, resulting in a new array of rounded numbers.

Frequently Asked Questions

Q1: What is the difference between Math.round(), Math.ceil(), and Math.floor()?

  • Math.round(): Rounds a number to the nearest integer.
  • Math.ceil(): Rounds a number up to the next integer.
  • Math.floor(): Rounds a number down to the previous integer.

Q2: How do I round a number to two decimal places?

You can use the following function:

function roundToTwoDecimalPlaces(number) {
  return Math.round(number * 100) / 100;
}

console.log(roundToTwoDecimalPlaces(3.14159)); // Output: 3.14

Q3: What happens if the number is exactly halfway between two integers?

If the number is exactly halfway between two integers, Math.round() rounds it to the nearest even integer. For example:

console.log(Math.round(2.5)); // Output: 2
console.log(Math.round(3.5)); // Output: 4

Q4: Can I use Math.round() with negative numbers?

Yes, Math.round() works with negative numbers in the same way as it works with positive numbers. For example:

console.log(Math.round(-2.3)); // Output: -2
console.log(Math.round(-2.6)); // Output: -3

Q5: What if I want to round a number to the nearest multiple of 10 or 100?

You can use the following function to round a number to the nearest multiple of a specified value:

function roundToMultiple(number, multiple) {
  return Math.round(number / multiple) * multiple;
}

console.log(roundToMultiple(25, 10)); // Output: 30
console.log(roundToMultiple(140, 100)); // Output: 100

In this example, the roundToMultiple function rounds the number to the nearest multiple of the specified value.

Conclusion

The Math.round() function in JavaScript is a powerful tool for rounding numbers to the nearest integer or to a specific decimal place. By understanding how it works and how to use it in different scenarios, you can make your code more precise and accurate.

Index
Scroll to Top