Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 30-May-16 18:53pm
v2

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[^]
 
Share this answer
 
v2
Comments
murkalkiran 31-May-16 1:23am    
Thank you it worked for me Peter Leow
murkalkiran 31-May-16 1:43am    
how can i achieve this in c# if i am using dictionary <string ,="" double="">
for the double without going for loop
I have added the solution to the below link:

Edit fiddle - JSFiddle[^]

It's working.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900