EN
JavaScript - closest multiple to the indicated value (ceil round)
5 points
In this short article, we would like to show how to compute the closest multiple to the indicated value (ceil round) using JavaScript.
xxxxxxxxxx
1
const findClosestMultiple = (base, value) => {
2
return base * Math.ceil(value / base);
3
};
4
5
6
// Usage example:
7
8
// // the closest ceiling multiple of 5 to the indicated value
9
console.log(findClosestMultiple(5, 0)); // 0
10
11
console.log(findClosestMultiple(5, 1)); // 5
12
console.log(findClosestMultiple(5, 3)); // 5
13
console.log(findClosestMultiple(5, 5)); // 5
14
15
console.log(findClosestMultiple(5, 6)); // 10
16
console.log(findClosestMultiple(5, 8)); // 10
17
console.log(findClosestMultiple(5, 10)); // 10
18
19
console.log(findClosestMultiple(5, 11)); // 15
20
console.log(findClosestMultiple(5, 13)); // 15
21
console.log(findClosestMultiple(5, 15)); // 15