如何在javascript或jquery中设置千位分隔符 [英] How to set thousand separator in javascript or jquery
问题描述
我有号码,我必须申请一千个分隔符
ex如果我的号码
double num = 123454545.125871656
结果应该像
123,454,545.125871656
我的尝试:
num.toString()。replace(/ \B( ?=(\d {3})+(?!\d))/ g,,);
结果如果有纯正的话会变得正确整数值,如12345789
12,345,789
但是当我得到双倍价值时如123454545.125871656
i得到123,454,545.125,871,656 ---->这个worng
i have number for which i have to apply a thousand separator
ex if my number
double num = 123454545.125871656
the result should be like
123,454,545.125871656
What I have tried:
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
the result getting correct if have pure integer value like 12345789
12,345,789
but when i get double value like 123454545.125871656
i am getting 123,454,545.125,871,656 ---->this worng
推荐答案
你可以先取小数部分,正则表达整数部分,然后把它们放进去再次一起。
查看此内容: JavaScript数字格式 - 添加逗号 - mredkj.com [ ^ ]
You can take out the decimal part first, regex the integer part, then put them together again.
Check this out: JavaScript Number Format - Add Commas - mredkj.com[^]
我已将解决方案添加到以下链接:
编辑小提琴 - JSFiddle [ ^ ]
工作正常。
I have added the solution to the below link:
Edit fiddle - JSFiddle[^]
It's working.
这篇关于如何在javascript或jquery中设置千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!