public
Created

commafy

  • Download Gist
commafy.1.js
JavaScript
1 2 3 4 5 6 7 8
String.prototype.commafy = function () {
return this.replace(
/^(-?\d+)(\d{3}(\.\d+)?)$/,
function (_, a, b) {
return a.commafy() + ',' + b;
}
);
};
commafy.2.js
JavaScript
1 2 3
Number.prototype.commafy = function () {
return this.toString().commafy();
};
commafy.stepThrough.1.js
JavaScript
1
'-1234567.89'.commafy()
commafy.stepThrough.2.js
JavaScript
1
'-1234'.commafy() + ',' + '567.89'
commafy.stepThrough.3.js
JavaScript
1
'-1'.commafy() + ',' + '234' + ',' + '567.89'
commafy.stepThrough.4.js
JavaScript
1
'-1' + ',' + '234' + ',' + '567.89'
commafy.stepThrough.5.js
JavaScript
1
'-1,234,567.89'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.