Solved

Format a decimal with Culture Info and thousand separators

Posted on 2009-04-04
1
5,066 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now