Last active
August 29, 2015 14:08
-
-
Save krist00fer/0d81cff982254e36be07 to your computer and use it in GitHub Desktop.
Add leading zeros (character or string) to number
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
// Pad number i left with c until n characters or more | |
// padLeft(1, 3); // '001' | |
// padLeft(12, 3); // '012' | |
// padLeft(1234, 3); // '1234' | |
// padLeft(1, 3, 'x'); // 'xx1' | |
function padLeft(i, n, c){ | |
return Array(n - String(i).length + 1).join(c||'0') + i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment