Skip to content

Instantly share code, notes, and snippets.

@robhurring
Created February 18, 2009 19:31
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 robhurring/66495 to your computer and use it in GitHub Desktop.
Save robhurring/66495 to your computer and use it in GitHub Desktop.
Allow strings to number_to_currency style comma seperate in javascript
// Allow strings to number_to_currency style comma seperate
String.prototype.commafy = function () {
return this.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
});
}
// Convenience method for numbers
Number.prototype.commafy = function () {
return String(this).commafy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment