Created
September 29, 2019 18:04
-
-
Save aleclarson/8b8edb06fa9233901df732fb87304f23 to your computer and use it in GitHub Desktop.
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
function roundToStep(n: number, step: number) { | |
n = Math.round(n / step) * step | |
// Determine the number of decimals to round. | |
let p = 0 | |
if (n % 1) { | |
const s = String(n) | |
p = s.length - s.indexOf('.') - 1 | |
} | |
if (p) { | |
const q = Math.pow(10, p) | |
return Math.round(q * n + q / 1e16) / q | |
} | |
return n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment