/ 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.
Useful for grabbing input values to format then display, remove the comma and convert back to number to do math.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var removeComma = function(str){ return str.replace(/,/g, '').trim(); } var addComma = function(str){ return str.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }