Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created April 25, 2017 13:25
Show Gist options
  • Save andreasvirkus/57dcad6d2d80d4a194c480c9dd2b811c to your computer and use it in GitHub Desktop.
Save andreasvirkus/57dcad6d2d80d4a194c480c9dd2b811c to your computer and use it in GitHub Desktop.
/**
* 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