Last active
August 29, 2015 14:12
-
-
Save vilicvane/9156ed5544e87c0998c7 to your computer and use it in GitHub Desktop.
Format number with leading zeros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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