Solved

Format a decimal with Culture Info and thousand separators

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

19 Experts available now in Live!

Get 1:1 Help Now