Skip to content

Instantly share code, notes, and snippets.

@vilicvane
Last active August 29, 2015 14:12
Show Gist options
  • Save vilicvane/9156ed5544e87c0998c7 to your computer and use it in GitHub Desktop.
Save vilicvane/9156ed5544e87c0998c7 to your computer and use it in GitHub Desktop.
Format number with leading zeros
// relatively safe
function formatSafely(num, digits) {
return (Array(digits).join('0') + Math.floor(num)).substr(-digits);
}
// or quick
function formatQuickly(num, digits) {
return (Array(digits).join('0') + num).substr(-digits);
}
// or inline
var formatted = ('00' + 7).substr(-3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment