How to Round a Number to the Nearest Multiple of N in JavaScript
A common requirement in programming is to round a number not just to the nearest integer, but to the nearest multiple of a specific value, such as 5, 10, or 50. This is useful for tasks like binning data, simplifying calculations, or formatting values for display.
This guide will teach you a single, powerful, and reusable mathematical trick to round any number to the nearest multiple of N. We will cover rounding to the nearest multiple, as well as always rounding up or down.
The Core Method: The "Divide, Round, Multiply" Trick
The logic for rounding to any multiple is a simple, three-step mathematical process:
- Divide: Divide the number you want to round by the multiple (
N) you are targeting. - Round: Round the result of the division to the nearest whole number using a standard
Mathfunction (round,ceil, orfloor). - Multiply: Multiply the rounded result back by the multiple (
N).
This simple algorithm is the foundation for all the examples in this guide.
Rounding to the Nearest Multiple of N
This is the most common use case, where you want to find the closest multiple, whether it's higher or lower. For this, we use Math.round().
Problem: you have a number and you want to round it to the nearest 5.
12should become10.13should become15.
Solution: this function encapsulates the "Divide, Round, Multiply" logic.
function roundToNearest(num, multiple) {
return Math.round(num / multiple) * multiple;
}
// Example Usage: Round to the nearest 5
console.log(roundToNearest(12, 5)); // Output: 10
console.log(roundToNearest(13, 5)); // Output: 15
console.log(roundToNearest(32.5, 5)); // Output: 35
How it works for 12:
- Divide:
12 / 5 = 2.4 - Round:
Math.round(2.4) = 2 - Multiply:
2 * 5 = 10
Rounding UP to the Nearest Multiple of N
If you always need to round up to the next highest multiple, you use Math.ceil() (for "ceiling").
function roundUpToNearest(num, multiple) {
return Math.ceil(num / multiple) * multiple;
}
// Example Usage: Round up to the nearest 10
console.log(roundUpToNearest(71, 10)); // Output: 80
console.log(roundUpToNearest(79.9, 10)); // Output: 80
console.log(roundUpToNearest(-44, 10)); // Output: -40
How it works for 71:
- Divide:
71 / 10 = 7.1 - Round Up:
Math.ceil(7.1) = 8 - Multiply:
8 * 10 = 80
Rounding DOWN to the Nearest Multiple of N
If you always need to round down to the nearest multiple, you use Math.floor().
function roundDownToNearest(num, multiple) {
return Math.floor(num / multiple) * multiple;
}
// Example Usage: Round down to the nearest 100
console.log(roundDownToNearest(349, 100)); // Output: 300
console.log(roundDownToNearest(399, 100)); // Output: 300
console.log(roundDownToNearest(-351, 100)); // Output: -400
How it works for 349:
- Divide:
349 / 100 = 3.49 - Round Down:
Math.floor(3.49) = 3 - Multiply:
3 * 100 = 300
Practical Examples (5, 10, 100)
Using the generic functions from above, you can easily create specific utility functions for common multiples.
// Generic rounding function
function roundToNearest(num, multiple) {
return Math.round(num / multiple) * multiple;
}
// Specific utility functions
let roundToNearest5 = (num) => roundToNearest(num, 5);
let roundToNearest10 = (num) => roundToNearest(num, 10);
let roundToNearest100 = (num) => roundToNearest(num, 100);
// Example Usage:
console.log(roundToNearest5(13)); // Output: 15
console.log(roundToNearest10(46)); // Output: 50
console.log(roundToNearest100(350)); // Output: 400
Conclusion
Rounding a number to a specific multiple is a simple mathematical operation that can be encapsulated in a single, reusable function.
- The core logic is always
Math.roundMethod(num / multiple) * multiple. - Use
Math.round()to round to the nearest multiple. - Use
Math.ceil()to always round up to the next multiple. - Use
Math.floor()to always round down to the nearest multiple.
By creating a generic function, you can handle any rounding requirement with clean, readable, and predictable code.