Created
April 25, 2017 13:25
-
-
Save andreasvirkus/57dcad6d2d80d4a194c480c9dd2b811c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Round the given number to the nearest incrementation of the provided stepper. | |
*/ | |
const roundToNearest = function(number, nearest) { | |
const rounded = number + nearest/2 - (number+nearest/2) % nearest; | |
return rounded; | |
}; | |
const nearest = 0.25; | |
console.log(roundToNearest(0.28, nearest)); // > 0.25 | |
console.log(roundToNearest(0.44, nearest)); // > 0.50 | |
console.log(roundToNearest(44, 3)); // > 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment