Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 5x 1x 5x 20x | /**
* Rounds a number to a multiple.
*
* @example
* Round to multiple of `5`
* ```
* const roundTo5 = roundToMultiple(5);
*
* console.log(roundTo5(4)); // 5
*
* console.log(roundTo5(-3)); // -5
* ```
*
* @example
* Round to `-3`, `2`, `7`, ...
* ```
* const roundTo5Plus2 = roundToMultiple(5, 2);
*
* console.log(roundTo5Plus2(0)); // 2
*
* console.log(roundTo5Plus2(8)); // 7
*
* console.log(roundTo5Plus2(-5)); // -3
* ```
*
* @param multiple - the multiple to round to
* @param shift - optional shift to apply
* @returns the rounded number
*/
export const roundToMultiple =
(multiple: number, shift = 0) =>
(num: number): number => {
return Math.round((num - shift) / multiple) * multiple + shift;
};
|