Skip to content

Instantly share code, notes, and snippets.

@Lobstrosity
Created January 21, 2012 21:37
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 Lobstrosity/1654123 to your computer and use it in GitHub Desktop.
Save Lobstrosity/1654123 to your computer and use it in GitHub Desktop.
commafy
String.prototype.commafy = function () {
return this.replace(
/^(-?\d+)(\d{3}(\.\d+)?)$/,
function (_, a, b) {
return a.commafy() + ',' + b;
}
);
};
Number.prototype.commafy = function () {
return this.toString().commafy();
};
'-1234567.89'.commafy()
'-1234'.commafy() + ',' + '567.89'
'-1'.commafy() + ',' + '234' + ',' + '567.89'
'-1' + ',' + '234' + ',' + '567.89'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment