C# doubles show comma instead of period
Asked Answered
C

3

10

I almost have the same problem as the guy in this thread:

Convert Float that has period instead of comma?

So that my

double x = 234.4;
string y = x.ToString();

I get y == "234,4";

Even worse ... Double.Parse("234.4") throws an exception.

I have written alot of code before I was asked to use period instead of comma, so I would prefer to have some way to change my CultureInfo at a global level.

Is there some setting in the projects that I can do?

I've tried:

        Application.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

They kind of work. They work for most part of the application, but all controls that reside in the tabs of my TabControl still wants to use my computers Localized CultureInfo.

Any Ideas on how to solve this?

Coverture answered 6/2, 2009 at 0:27 Comment(2)
Normally setting the CultureInfo on the thread should be enough. Maybe you're running the code that sets the cultureinfo on another thread than the GUI one?Doctorate
Huh! That actually was the case. Thanks! I thought setting Application.CurrentCulture would affect all threads. ....and it does, but only if I set it before I create my form for some reason. I had placed it below the form creation (but before run). Anyways, case closed! Thanks again!Coverture
C
7

Thanks to Florin Sabaus comment I found the solution, which was to place

        Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

in Main() right before I created my form.

Now I won't have to change all my .ToString() and Double.Parse() :-) Yey!

Coverture answered 6/2, 2009 at 8:10 Comment(0)
D
11

You could try to use

double.Parse("...", CultureInfo.InvariantCulture)

and

x.ToString(CultureInfo.InvariantCulture)

in the parts of the program that you are positive you need to have decimal period instead of comma or other regional settings dependent decimal separator.

Hope it helps.

Doctorate answered 6/2, 2009 at 0:37 Comment(0)
C
7

Thanks to Florin Sabaus comment I found the solution, which was to place

        Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

in Main() right before I created my form.

Now I won't have to change all my .ToString() and Double.Parse() :-) Yey!

Coverture answered 6/2, 2009 at 8:10 Comment(0)
J
1

Try this solution: http://www.codeproject.com/KB/cs/Change_App_Culture.aspx

Jackqueline answered 6/2, 2009 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.