如何在页面加载后使用jquery在数字中添加逗号格式 [英] How to add commas format in number after page load using jquery
问题描述
我有一页有银行资金摘要详情,我从API获得所有这些价值。这些值的格式不像50000.3645656。
I have one page which is having bank funds summary details and i am getting all these value from API. These values are not formatted for example like 50000.3645656.
所以这是我的页面结构,如下所示
So here is my page structure given below
<span class="format">50000.3645656</span>
<span class="format">50000.3645656</span>
<span class="format">50000.3645656</span>
<span class="format">50000.3645656</span>
<span class="format">50000.3645656</span>
所以我想要的是,我想用逗号形成两个小数点来格式化所有这些值像50,000.36。
So what i want exactly is , I want to format all these value with comma formated with two decimal point like 50,000.36.
如何在jquery方法中使用jquery和css类格式加载页面后执行此操作。
How can i do this after page load using jquery and css class format in jquery method.
推荐答案
据我所知,你不能用纯CSS做到这一点。
As far as I know, you can't do this using pure CSS.
但是,您可以使用JavaScript函数为您添加逗号。
However, you could use a JavaScript function to add commas for you.
我过去曾使用过这个逗号:
I have used this one in the past:
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;
}
然后执行你的jquery:
And then do your jquery:
$(function(){
$(".format").each(function(c, obj){
$(obj).text(addCommas(parseFloat($(obj).text()).toFixed(2)));
});
});
这篇关于如何在页面加载后使用jquery在数字中添加逗号格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!