Skip to content

Instantly share code, notes, and snippets.

@mihailgaberov
Created September 5, 2021 07:43
Show Gist options
  • Save mihailgaberov/df2c0d91313c876d1eae850d7f7c3e7a to your computer and use it in GitHub Desktop.
Save mihailgaberov/df2c0d91313c876d1eae850d7f7c3e7a to your computer and use it in GitHub Desktop.
Returns the number rounded to the nearest interval - use `Math.round` if you need upper rounding.
const levels = [
[1001, 1],[1000.5, 1],[1000, 1]
]
/**
* Returns the number rounded to the nearest interval.
* Example:
*
* roundToNearest(80, 100); // 100
* roundToNearest(25, 15); // 30
*
* @param {number} value The number to round
* @param {number} interval The numeric interval to round to
* @return {number}
*/
function roundToNearest(value, interval) {
// return Math.round(value/interval) * interval;
return Math.floor(value/interval) * interval;
}
console.log(roundToNearest(1000.5, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment