jq金钱如何加千分位,使用Javascript或jQuery向总数添加千位分隔符?

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 thousands. Initially, you'll note, there are commas in the numbers being added. So that the function will add them, they are removed. How do I add the commas back in there?

function sumOfColumns(tableID, columnIndex, hasHeader) {

var tot = 0;

$("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""))

.children("td:nth-child(" + columnIndex + ")")

.each(function() {

tot += parseInt($(this).html().replace(',', ''));

});

return "Total Pounds Entered : " + tot;

}

解决方案

The $(this).html().replace(',', '') shouldn't actually modify the page. Are you sure the commas are being removed in the page?

If it is, this addCommas function should do the trick.

function addCommas(nStr) {

nStr += '';

var x = nStr.split('.');

var x1 = x[0];

var x2 = x.length > 1 ? '.' + x[1] : '';

var rgx = /(\d+)(\d{3})/;

while (rgx.test(x1)) {

x1 = x1.replace(rgx, '$1' + ',' + '$2');

}

return x1 + x2;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值