Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Created October 20, 2012 15:20
Show Gist options
  • Save alexanderscott/3923551 to your computer and use it in GitHub Desktop.
Save alexanderscott/3923551 to your computer and use it in GitHub Desktop.
comma-separated thousands fxn
/* comma-separated thousands */
// if no decimals
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// if more than 3 decimal places
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment