Solved

ToString() on Int or Double using current culture

Posted on 2016-08-27
3
38 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
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Sandar Aye
Comment Utility
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 49

Accepted Solution

by:
Gustav Brock earned 500 total points (awarded by participants)
Comment Utility
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 49

Expert Comment

by:Gustav Brock
Comment Utility
The solution asked for should be culture neutral.

/gustav
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

11 Experts available now in Live!

Get 1:1 Help Now