Skip to content

Instantly share code, notes, and snippets.

@jlleblanc
Created June 21, 2010 15:07
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 jlleblanc/446977 to your computer and use it in GitHub Desktop.
Save jlleblanc/446977 to your computer and use it in GitHub Desktop.
function comma_format_number (value) {
value = value.replace(/,/g, '');
var lead = value.length % 3;
var lead_segment = value.substring(0, lead);
value = value.substring(lead, value.length);
value = value.match(/(\d{3})/g);
if (value != null && value.length > 0) {
value = value.join(',');
if (lead_segment.length > 0) {
value = lead_segment + ',' + value;
};
} else {
value = lead_segment;
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment