Skip to content

Instantly share code, notes, and snippets.

@robhurring

robhurring/any.js

Created Feb 18, 2009
Embed
What would you like to do?
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
You can’t perform that action at this time.