Skip to content

Instantly share code, notes, and snippets.

@parisminton
Created July 8, 2013 17:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parisminton/5950879 to your computer and use it in GitHub Desktop.
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);'
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