Tested Approach:
number.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');Short solution #2:
Alternate Solution:
number.toFixed(2).replace(/./g, function(c, i, a) {
return i && c !== "." && !((a.length - i) % 3) ? ',' + c : c;
});
Example:
5543.45 will be converted to 5,543.45.
number.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');Short solution #2:
Alternate Solution:
number.toFixed(2).replace(/./g, function(c, i, a) {
return i && c !== "." && !((a.length - i) % 3) ? ',' + c : c;
});
Example:
5543.45 will be converted to 5,543.45.