Skip to content

Instantly share code, notes, and snippets.

@xiujunma
Created June 25, 2013 10: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 xiujunma/5857383 to your computer and use it in GitHub Desktop.
Save xiujunma/5857383 to your computer and use it in GitHub Desktop.
add commas to currency
currency_comma = function (str) {
str += '';
var arr = str.split('.'),
integer = arr[0],
fraction = arr.length > 1 ? '.' + arr[1] : '';
var regex = /(\d+)(\d{3})/;
while (regex.test(integer)) {
integer = integer.replace(regex, '$1' + ',' + '$2');
}
return integer + fraction;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment