Adding/remove commas in digits

/ Published in: JavaScript
Save to your folder(s)

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.

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.