Last active
August 29, 2015 14:19
-
-
Save lamchau/40d8f6456b6d9243ad6c to your computer and use it in GitHub Desktop.
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
function pad(n, count) { | |
// http://stackoverflow.com/a/15398371 | |
var length = (Math.log(Math.abs(n + 1)) * 0.43429448190325176 | 0) + 1; | |
count = Math.max(count - length, 0); | |
if (n < 10) { | |
return n; | |
} | |
// http://stackoverflow.com/a/5450113 | |
var result = ''; | |
var pattern = '0'; | |
while (count > 1) { | |
if (count & 1) { | |
result += pattern; | |
} | |
count >>= 1; | |
pattern += pattern; | |
} | |
return result + pattern + n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment