Created
February 18, 2009 19:31
-
-
Save robhurring/66495 to your computer and use it in GitHub Desktop.
Allow strings to number_to_currency style comma seperate in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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