Created
September 5, 2021 07:43
-
-
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.
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
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