I have a function that sums a column of data in an html table. It does so admirably only I would like to have it put the commas in there that are needed to separate the thou
a recursive solution:
function thousands(amount) {
if( /\d{3}\d+/.test(amount) ) {
return thousands(amount.replace(/(\d{3}?)(,|$)/, ',$&'));
}
return amount;
}
another split solution:
function thousands (amount) {
return amount
// reverse string
.split('')
.reverse()
.join('')
// grouping starting by units
.replace(/\d{3}/g, '$&,')
// reverse string again
.split('')
.reverse()
.join('');
}