Created
July 8, 2013 17:44
-
-
Save parisminton/5950879 to your computer and use it in GitHub Desktop.
This function will automatically insert commas after the thousands place of any number. Returns a string. Useful for calculations that appear in copy. Call 'commaify(some_dynamic_integer);'
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 commaify (num) { | |
var num_string, | |
i, | |
len, | |
threes = [], | |
remainders; | |
num_string = num.toString(); | |
len = num_string.length; | |
remainders = len % 3; | |
if (remainders != 0) { | |
threes.push(num_string.substr(0, remainders)); | |
} | |
for (i = remainders; i < len; i += 3) { | |
threes.push(num_string.substr(i, 3)); | |
} | |
return threes.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment