Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ToString() on Int or Double using current culture

Posted on 2016-08-27
3
Medium Priority
?
87 Views
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.

Thanks
0
Comment
Question by:bamapie
[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
  • 2
3 Comments
 
LVL 1

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


            Response.Write("<br>");


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

            Response.Write("&nbsp;&nbsp;&nbsp;");

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

Open in new window

0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 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

/gustav
0
 
LVL 52

Expert Comment

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

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

610 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