Skip to content

Instantly share code, notes, and snippets.

@danielrw7
Created February 15, 2019 16:19
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 danielrw7/6e58720f4e44fd4c3477366efa3f9427 to your computer and use it in GitHub Desktop.
Save danielrw7/6e58720f4e44fd4c3477366efa3f9427 to your computer and use it in GitHub Desktop.
Round padded
function roundPadded(n, d = 0) {
const div = Math.pow(10, d)
let res = (Math.round(n * div) / div).toString().split('.')
const whole = res[0]
if (d <= 0) {
return whole
}
let dec = res[1] || ''
if (d > 0 && dec.length < d) {
dec += '0'.repeat(d - dec.length)
}
return whole + '.' + dec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment