ToString() on Int or Double using current culture

Posted on 2016-08-27
Last Modified: 2016-09-16
I'd like to be able to call ToString() on a variable without specifying a culture (so, using System.Globalization.CultureInfo.CurrentCulture in some way) that produces comma-separated integers for those of us in the U.S.:

123456789.123 => 123,456,789.123

but allows partners in Europe to see things according to their UI culture.  So, probably something like this:

123456789.123 => 123.456.789,123

Basically, with commas and decimals reversed.

I don't believe I should have to specify the target culture. I always want to use the UI culture.  Every damn example I can find seems to be targeting "de-DE" or something specific.  Or the result lacks the separator commas that I'm wanting for US-based users.

Question by:bamapie
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
  • 2

Expert Comment

by:Sandar Aye
ID: 41773459
Hello Bamapie,

Please take a look the following example code. Feel free to contact me if you have any questions.

            double value = 123456789.123;
            string specifier;

            CultureInfo culture;
            specifier = "G"; // Use standard numeric format specifiers.
            culture = CultureInfo.CreateSpecificCulture("en-US");
            Response.Write(value.ToString(specifier, culture)); // Result 123456789.123

            Response.Write("   ");

            culture = CultureInfo.CreateSpecificCulture("de-DE");
            Response.Write(value.ToString(specifier, culture)); // Result 123456789,123


            specifier = "C"; // Use standard currency format specifiers.
            culture = CultureInfo.CreateSpecificCulture("en-US");
            Response.Write(value.ToString(specifier, culture)); // Result $123,456,789.12


            culture = CultureInfo.CreateSpecificCulture("de-DE");
            Response.Write(value.ToString(specifier, culture)); // Result 123.456.789,12 €

Open in new window

LVL 50

Accepted Solution

Gustav Brock earned 500 total points (awarded by participants)
ID: 41773627
All you should need is to specify the number of decimals:
double number = 123456789.123;
string formattedNumber = number.ToString ("n3");

// -> 123.456.789,123 in most of Europe.

Open in new window

LVL 50

Expert Comment

by:Gustav Brock
ID: 41801197
The solution asked for should be culture neutral.


Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

687 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