Created
January 15, 2015 06:48
-
-
Save jfhernandeze/f08952c20c045d321737 to your computer and use it in GitHub Desktop.
Zerofill for javascript
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 zeroFill(number, width) { | |
width -= number.toString().length; | |
if (width > 0) { | |
return new Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number; | |
} | |
return number + ""; // always return a string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment