如何在页面加载后使用jquery在数字中添加逗号格式 [英] How to add commas format in number after page load using jquery

查看:106
本文介绍了如何在页面加载后使用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)));
   });
});

JSfiddle Demonstration

这篇关于如何在页面加载后使用jquery在数字中添加逗号格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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