Created
December 8, 2017 03:45
-
-
Save emilio-martinez/49c4e0c5bc5b1cf2ac6a670b3073ddad to your computer and use it in GitHub Desktop.
roundToMultipleOf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function roundToMultipleOf(multipleOf) { | |
const factor = 1 / multipleOf; | |
return num => Math.round(num * factor) / factor; | |
} | |
/** | |
* EXAMPLE | |
* Rounding to the closest multiple of five | |
*/ | |
const roundToMultipleOfFive = roundToMultipleOf(5); | |
roundToMultipleOfFive(-32.31) // => -30 | |
roundToMultipleOfFive(12.10) // => 10 | |
roundToMultipleOfFive(26.29) // => 25 | |
roundToMultipleOfFive(47.431) // => 45 | |
roundToMultipleOfFive(62.111) // => 60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment