Solved

Format a decimal with Culture Info and thousand separators

Posted on 2009-04-04
1
5,106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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