Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Created July 6, 2009 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atesgoral/141616 to your computer and use it in GitHub Desktop.
Save atesgoral/141616 to your computer and use it in GitHub Desktop.
/**
* Left-pad a number with zeros so that it's at least the given
* number of characters long
* @param n The number to pad
* @param len The desired length
*/
function leftPad(n, len) {
return (new Array(len - String(n).length + 1)).join("0").concat(n);
}
alert(leftPad(42, 6)); // Gives you "000042"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment