Skip to content

Instantly share code, notes, and snippets.

@krist00fer
Last active August 29, 2015 14:08
Show Gist options
  • Save krist00fer/0d81cff982254e36be07 to your computer and use it in GitHub Desktop.
Save krist00fer/0d81cff982254e36be07 to your computer and use it in GitHub Desktop.
Add leading zeros (character or string) to number
// 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