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