Skip to content

Instantly share code, notes, and snippets.

@mapsam
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapsam/b4ace719413a3ad6c82c to your computer and use it in GitHub Desktop.
Save mapsam/b4ace719413a3ad6c82c to your computer and use it in GitHub Desktop.
commas
function addCommas(n) {
var str = n.toString(),
len = str.length,
num = str.slice(0, len%3);
for(i=0;i<Math.floor(len/3);i++) {
num = num + ',' + str.substr(len%3+(3*i), 3);
}
if(num.slice(0,1)===',') {
num = num.slice(1);
}
return num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment