Skip to content

Instantly share code, notes, and snippets.

@emilio-martinez
Created December 8, 2017 03:45
Show Gist options
  • Save emilio-martinez/49c4e0c5bc5b1cf2ac6a670b3073ddad to your computer and use it in GitHub Desktop.
Save emilio-martinez/49c4e0c5bc5b1cf2ac6a670b3073ddad to your computer and use it in GitHub Desktop.
roundToMultipleOf
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