如何用逗号显示大数字? HTML
How do I display large numbers with commas? HTML
我想用逗号更好地显示大数字。所以如果数字是 123456789,它会显示 123,456,789。我环顾四周,但只发现无法按我想要的方式工作的代码,所以我希望能在这里找到一些帮助。另外,我希望它也是动态的,所以逗号会随着数字的变化而变化。
我要影响的号码id="value".
应该就这些了,我不认为我遗漏了什么。所以我再次希望带有 id="value" 的数字在需要时引入逗号。如果您需要更多信息,请告诉我!
此处已回答:
How to print a number with commas as thousands separators in JavaScript
如果您对阅读上面的答案不感兴趣,给出的代码是这样的:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
如果您使用的是 jquery,请使用类似的东西:
var val = parseInt($('#value').text());
//Use the code in the answer above to replace the commas.
val = numberWithCommas(val);
$('#value').text(val);
您可以使用 toLocaleString
:
num.toLocaleString('en', {useGrouping:true})
toLocaleString 有一个更简单的语法:
Number(x).toLocaleString();
这让您可以将数字作为变量放入,而不是将数字转换为对象。
我想用逗号更好地显示大数字。所以如果数字是 123456789,它会显示 123,456,789。我环顾四周,但只发现无法按我想要的方式工作的代码,所以我希望能在这里找到一些帮助。另外,我希望它也是动态的,所以逗号会随着数字的变化而变化。
我要影响的号码id="value".
应该就这些了,我不认为我遗漏了什么。所以我再次希望带有 id="value" 的数字在需要时引入逗号。如果您需要更多信息,请告诉我!
此处已回答:
How to print a number with commas as thousands separators in JavaScript
如果您对阅读上面的答案不感兴趣,给出的代码是这样的:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
如果您使用的是 jquery,请使用类似的东西:
var val = parseInt($('#value').text());
//Use the code in the answer above to replace the commas.
val = numberWithCommas(val);
$('#value').text(val);
您可以使用 toLocaleString
:
num.toLocaleString('en', {useGrouping:true})
toLocaleString 有一个更简单的语法:
Number(x).toLocaleString();
这让您可以将数字作为变量放入,而不是将数字转换为对象。