?
Solved

ToString() on Int or Double using current culture

Posted on 2016-08-27
3
Medium Priority
?
82 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 51

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 51

Expert Comment

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

/gustav
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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