Last active
August 29, 2015 14:03
-
-
Save jeremiahajohnson/aa18284695c383be50c9 to your computer and use it in GitHub Desktop.
Javascript zero padding
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 pad(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
pad(10, 4); // 0010 | |
pad(9, 4); // 0009 | |
pad(123, 4); // 0123 | |
pad(10, 4, '-'); // --10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment