Skip to content

Instantly share code, notes, and snippets.

@gerbenjacobs
Last active December 7, 2017 13:24
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 gerbenjacobs/5609334 to your computer and use it in GitHub Desktop.
Save gerbenjacobs/5609334 to your computer and use it in GitHub Desktop.
A javascript function to zeropad (or prefill a number with any character) and return a padded string.
function zeropad(number, amount) {
if (number >= Math.pow(10, amount)) {
return number;
}
return (Array(amount).join(0) + number).slice(-amount);
}
zeropad(2); // 2
zeropad(2, 1); // 2
zeropad(2, 2); // 02
zeropad(2, 3); // 002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment