Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to round a number in JavaScript

In this blog post, we will learn how to round a number in JavaScript. Rounding numbers is a common task in programming, especially when dealing with calculations, measurements, and displaying numerical values to users. We will cover different methods to round numbers in JavaScript and dive into their nuances. By the end of this post, you should have a good understanding of the various ways to round numbers in JavaScript and when to use them.

What is rounding?

Rounding is the process of converting a number to an approximate value that is easier to understand and work with. When we round a number, we typically reduce the number of decimal places, making it simpler to read, display, or use in further calculations. For example, if we have the number 3.141592, we might round it to 3.14 or even just 3, depending on the level of approximation we want.

Let's consider a real-life example: Imagine you are measuring the length of a table, and your measuring tape shows the length as 4.678 meters. If someone asks you how long the table is, you might say it's about 4.7 meters long. Here, you have rounded the original measurement to a more manageable number.

Rounding in JavaScript

JavaScript provides several built-in methods to round numbers. In this post, we will explore the following methods:

  1. Math.round()
  2. Math.floor()
  3. Math.ceil()
  4. toFixed()
  5. Custom rounding functions

1. Math.round()

Math.round() is a built-in JavaScript function that rounds a number to the nearest integer. If the decimal part of the number is equal to or greater than 0.5, it rounds up to the nearest whole number; otherwise, it rounds down.

Here's an example:

let num1 = 3.4;
let num2 = 3.5;
let num3 = 3.6;

console.log(Math.round(num1)); // Output: 3
console.log(Math.round(num2)); // Output: 4
console.log(Math.round(num3)); // Output: 4

2. Math.floor()

Math.floor() is another built-in JavaScript function that rounds a number down to the nearest integer, regardless of its decimal part.

Here's an example:

let num1 = 3.4;
let num2 = 3.5;
let num3 = 3.6;

console.log(Math.floor(num1)); // Output: 3
console.log(Math.floor(num2)); // Output: 3
console.log(Math.floor(num3)); // Output: 3

3. Math.ceil()

Math.ceil() is the counterpart to Math.floor(). It rounds a number up to the nearest integer, regardless of its decimal part.

Here's an example:

let num1 = 3.4;
let num2 = 3.5;
let num3 = 3.6;

console.log(Math.ceil(num1)); // Output: 4
console.log(Math.ceil(num2)); // Output: 4
console.log(Math.ceil(num3)); // Output: 4

4. toFixed()

toFixed() is a method available on all JavaScript numbers. It takes a single argument, the number of decimal places to round the number to, and returns a string representing the rounded number. If you need the result as a number, you can use parseFloat() or Number() to convert the string back to a number.

Keep in mind that toFixed() uses rounding similar to Math.round() when deciding whether to round up or down.

Here's an example:

let num = 3.14159;

console.log(num.toFixed(2)); // Output: "3.14" (as a string)
console.log(parseFloat(num.toFixed(2))); // Output: 3.14 (as a number)
console.log(Number(num.toFixed(2))); // Output: 3.14 (as a number)

5. Custom rounding functions

In some cases, you might need more control over the rounding process. To achieve this, you can create custom rounding functions. Let's create a function that rounds a number to a specified number of decimal places.

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

let num = 3.14159;

console.log(customRound(num, 2)); // Output: 3.14
console.log(customRound(num, 3)); // Output: 3.142

In this function, we multiply the input number by 10 raised to the power of the desired decimal places, then round the result using Math.round(). Finally, we divide the rounded result by the same factor to get the final rounded number.

This custom function gives you more control over the rounding process, and you can modify it to implement different rounding rules if necessary.

When to use each method

Now that we've covered the various methods for rounding numbers in JavaScript, let's discuss when to use each one.

Use Math.round() when you want to round a number to the nearest whole number, following the standard rounding rules (0.5 and above rounds up, while below 0.5 rounds down).

Use Math.floor() when you want to round a number down to the nearest whole number, regardless of its decimal part.

Use Math.ceil() when you want to round a number up to the nearest whole number, regardless of its decimal part.

Use toFixed() when you want to round a number to a specific number of decimal places and don't mind the result being a string. Remember to convert the result back to a number if necessary.

Use a custom rounding function when you need more control over the rounding process or want to implement non-standard rounding rules.

Conclusion

In this blog post, we covered different methods for rounding numbers in JavaScript. We discussed the built-in functions Math.round(), Math.floor(), Math.ceil(), and toFixed(), as well as creating custom rounding functions for more control over the process. We also discussed when to use each method, depending on your specific needs.

Rounding numbers is an essential skill for any programmer, and understanding the various methods available in JavaScript will make your life easier when dealing with numerical data. Hopefully, this post has given you the knowledge and confidence to round numbers in JavaScript effectively.