I have a textbox, that is binded to a string variable. The input is 5.5 I need the string variable to calculate something but i noticed that float.parse(stringvariable) ignores the dot (makes it a 55).
Is there a way to fix this problem?
Thank you
I have a textbox, that is binded to a string variable. The input is 5.5 I need the string variable to calculate something but i noticed that float.parse(stringvariable) ignores the dot (makes it a 55).
Is there a way to fix this problem?
Thank you
This is due to your default culture info. To accept decimal points which are .
then can do this:
float f = float.Parse("float value as string", CultureInfo.InvariantCulture);
However if you're expecting ,
to be used then you can specify then you can specify a culture which does that like this:
float f = float.Parse("float value as string", new CultureInfo("fr-FR").NumberFormat);
Obviously can be any culture and don't have to use French!
As stated by Tom Dee, this issue is related to your System.Globalization.CultureInfo settings.
You can modify this for the current thread by setting:
i.e.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
or
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("it-IT");
to the CultureInfo object you need, as well as use the correct overload for the Parse method, previously highlighted.