Skip to content

Instantly share code, notes, and snippets.

@banterability
Created August 31, 2010 21:07
Show Gist options
  • Save banterability/559757 to your computer and use it in GitHub Desktop.
Save banterability/559757 to your computer and use it in GitHub Desktop.
Add commas to integers (2000000 -> 2,000,000) in javascript
var intcomma = function(value) {
// inspired by django.contrib.humanize.intcomma
var origValue = String(value);
var newValue = origValue.replace(/^(-?\d+)(\d{3})/, '$1,$2');
if (origValue == newValue){
return newValue;
} else {
return intcomma(newValue);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment