Reputation: 396
I'm trying to Convert the string "5.7" (from a textbox) in to a float like this:
float m = float.Parse(TextBox9.Text);
But I'm getting the following error:
System.FormatException: Input string was not in a correct format.
what is wrong please?
Upvotes: 0
Views: 1860
Reputation: 6896
float.Parse(Textbox9.Text, CultureInfo.InvariantCulture.NumberFormat);
Upvotes: 1
Reputation: 1923
You et the exception, because the text in the TextBox9 does not fit the "country-rules" for a correct decimal number. Usually this happens, if the dot represents a thousand seperator and not the decimal point. Maybe you can use:
float number = float.Parse(TextBox9.Text, CultureInfo.InvariantCulture);
or
float number = float.Parse(TextBox9.Text, Thread.CurrentThread.CurrentUICulture);
To avoid the exception you can use:
float number;
if (!float.TryParse(TextBox9.Text, out number))
MessageBox.Show("Input must be a decimal number.");
Upvotes: 0