Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created September 29, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleclarson/8b8edb06fa9233901df732fb87304f23 to your computer and use it in GitHub Desktop.
Save aleclarson/8b8edb06fa9233901df732fb87304f23 to your computer and use it in GitHub Desktop.
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