Avatar of bamapie
bamapie
 asked on

ToString() on Int or Double using current culture

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
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
Sandar Aye

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

ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

The solution asked for should be culture neutral.

/gustav
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck