Skip to content

Instantly share code, notes, and snippets.

@O-Zone
Created June 18, 2016 06:58
Show Gist options
  • Save O-Zone/3206ffba72c86d883f3febd79a68a2e5 to your computer and use it in GitHub Desktop.
Save O-Zone/3206ffba72c86d883f3febd79a68a2e5 to your computer and use it in GitHub Desktop.
Polyfill that makes you print out a Number in at least a certain number of digits. Examples: 7.minDigits(3) => '007'; 64.3.minDigits(4) => '0064.3'; 432.minDigits(2) => '432'
Number.prototype.minDigits = function (d) {
if ('number' !== typeof d) {
return this.toString();
}
var s = this.toString().split('.');
while (s[0].length < d) {
s[0] = '0' + s[0];
}
return s[0] + (s[1] ? '.' + s[1] : '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment