使用javascript的自动逗号 [英] Auto Comma using javascript

查看:57
本文介绍了使用javascript的自动逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
我想将数字显示为逗号分隔的字符串
(就像我们显示的金额12,34,675.67)

我有一个JavaScript函数,该函数在文本框的onkeyup事件上调用.
这是该功能:

Hi Friends,
I want to display numbers as comma separated string
(like we show amount 12,34,675.67)

I have a javascript function, which I call on onkeyup event of a textbox.
Here is that function :

function Comma(Num) 
{
     Num += '';
     Num = Num.replace(/,/g, '');

      x = Num.split('.');
      x1 = x[0];
      x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1))
         x1 = x1.replace(rgx, '$1' + ',' + '$2');
      
      return x1 + x2;
} 


此功能给我的金额为
123,456,984.00

现在,如果我回购了凭单,我已经使用
格式化了文本框,以设置金额.


This function gives me the amount as
123,456,984.00

Now, if I repoen my Voucher, I have formatted the textbox for amount using

.ToString("N")


向我显示该值为
12,34,67,567.00

如您所见,这两个函数的逗号位置有所不同.

如何获得相同的金额格式?
如果该值在小数点后四舍五入两位数,那就更好了.

任何帮助表示赞赏.

谢谢,
Lok ..


which shows me the value as
12,34,67,567.00

AS you can see, there is difference in comma placings of these two functions.

How can I achieve the same format for amount ??
And it will be a lot better, if the value is rounded by two digits after decimal point.

Any help is appreciated.

Thanks,
Lok..

推荐答案

1' + ' '
1' + ',' + '


2'); 返回 x1 + x2; }
2'); return x1 + x2; }


此功能给我的金额为
123,456,984.00

现在,如果我回购了凭单,我已经使用
格式化了文本框,以设置金额.


This function gives me the amount as
123,456,984.00

Now, if I repoen my Voucher, I have formatted the textbox for amount using

.ToString("N")


向我显示该值为
12,34,67,567.00

如您所见,这两个函数的逗号位置有所不同.

如何获得相同的金额格式?
如果该值在小数点后四舍五入两位数,那就更好了.

任何帮助表示赞赏.

谢谢,
乐..


which shows me the value as
12,34,67,567.00

AS you can see, there is difference in comma placings of these two functions.

How can I achieve the same format for amount ??
And it will be a lot better, if the value is rounded by two digits after decimal point.

Any help is appreciated.

Thanks,
Lok..


看看 http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx [ ^ ]
Take a look at Microscoft AJAX MaskedEdit Controls[^]
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx[^]


这篇关于使用javascript的自动逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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