Created
June 18, 2016 06:58
-
-
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'
This file contains hidden or 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
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