Skip to content

Instantly share code, notes, and snippets.

@jeremiahajohnson
Last active August 29, 2015 14:03
Show Gist options
  • Save jeremiahajohnson/aa18284695c383be50c9 to your computer and use it in GitHub Desktop.
Save jeremiahajohnson/aa18284695c383be50c9 to your computer and use it in GitHub Desktop.
Javascript zero padding
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