如何用逗号显示大数字? HTML [英] How do I display large numbers with commas? HTML

查看:219
本文介绍了如何用逗号显示大数字? HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用逗号更好地显示大量数字。所以如果数字是123456789,它会显示123,456,789。我看了看周围,但我只发现代码,只是不会按我想要的方式工作,所以我希望我可以在这里找到一些帮助。此外,我希望它也是动态的,所以逗号会随着数字的变化而改变。

I am wanting to display large numbers more nicely with commas. So if the number was say 123456789, it would display 123,456,789. I have looked around but I only found code that just wouldn't work the way I wanted so I was hoping I could find some help here. Also, I hope it is also dynamic, so the commas will change as the number changes.

我想要影响的数字有id =value。

The number that I want to affect has the id="value".

这应该是全部,我不认为我错过了任何东西。所以我再次希望带有id =value的数字在需要时引入逗号。

That should be all, I don't think I am missing anything. So again I want the number with an id="value" to have commas introduced when it's needed. If you need any more information please let me know!

推荐答案

这里回答了这个问题:

如何在JavaScript中用千位分隔符打印一个数字

如果您不想阅读上面的答案,给出的代码是这样的: p>

In case you're not interested in reading the answer above, the code given was this:

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

如果您使用的是jquery,请使用如下所示:

If you're using jquery use something like:

var val = parseInt($('#value').text());
//Use the code in the answer above to replace the commas.
val = numberWithCommas(val);
$('#value').text(val);

这篇关于如何用逗号显示大数字? HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆