Posted By

supawaza on 12/06/13


js number amount comma

Versions (?)

Adding/remove commas in digits

 / Published in: JavaScript

Add / Remove comma for number in the thousands. Ex: 1,000

Useful for grabbing input values to format then display, remove the comma and convert back to number to do math.

  1. var removeComma = function(str){
  2. return str.replace(/,/g, '').trim();
  3. }
  5. var addComma = function(str){
  6. return str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  7. }

Report this snippet  

You need to login to post a comment.