Created
February 15, 2019 16:19
-
-
Save postpostscript/6e58720f4e44fd4c3477366efa3f9427 to your computer and use it in GitHub Desktop.
Round padded
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 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