Solved

Format a decimal with Culture Info and thousand separators

Posted on 2009-04-04
1
5,096 Views
Last Modified: 2013-12-17
I have a value in the database that is stored using the en-US culture info number formatting:
20802.10549248

I want to display this value in my .Net C# web app using the user's CultureInfo.CurrentUICulture.NumberFormat, including the thousand separators.  But I also do not want to truncate any of the decimal places.

So, if the user's Thread.CurrentThread.CurrentUICulture is set to "en-US", I want the number to show as:
20,802.10549248

If the user's  Thread.CurrentThread.CurrentUICulture is set to "fr-BE", I want the number to show as:
20.802,10549248

I have only seen solutions that specify a certain number of decimal places, which would truncate my value.  Right now I have the code as follows:
return ((double)value).ToString(CultureInfo.CurrentUICulture.NumberFormat);
However, this doesn't insert the thousand separator as I want it to.

I also tried variations on:
return ((double)value).ToString("n", CultureInfo.CurrentUICulture.NumberFormat);
Which all insert the thousand separator, nut "n" truncates to 2 decimal places and any number (i.e. 0 or 2 or 10) after the "n" truncates the decimals or adds padding zeros.

Does anyone have a solution for this seemingly simple problem?

Thanks in advance!
0
Comment
Question by:chinacat456
1 Comment
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 24069083
Hi,

How about (you can use as many # as decimalholders as you like, they will only be shown if the digit is significant)

return ((double)value).ToString("#,0.#############", CultureInfo.CurrentUICulture.NumberFormat);

/peter
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question