0

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?

7
  • What value does the textbox hold Commented Jun 15, 2016 at 5:13
  • TextBox gets a value like 6.7 Commented Jun 15, 2016 at 5:15
  • Why are you not using TryParse method ? Commented Jun 15, 2016 at 5:16
  • 1
    What Culture you have? It possible your current culture have different decimal separator character Commented Jun 15, 2016 at 5:16
  • 1
    Try this: float m = float.Parse(TextBox9.Text, CultureInfo.InvariantCulture); CultureInfo.InvariantCulture has decimal separator set to .(dot) Commented Jun 15, 2016 at 5:18

2 Answers 2

1
float.Parse(Textbox9.Text, CultureInfo.InvariantCulture.NumberFormat);
Sign up to request clarification or add additional context in comments.

Comments

0

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.");

4 Comments

Thread.CurrentThread.CurrentUICulture used by default. Avoiding exception not very good approach
Exceptions is very important to get information something goes wrong. On the top layer of application you can handle Exception and show some information about it to the user. When you avoiding exception, then you didn't get information about error
I agrree with you in a multi layer design if e.g the input is checked in some model. But here we are in the top layer. Otherwise there would be no access to TextBox9. Additionally the requirement is to input a number. In this case for me the input of a non-number into the textbox is not an exceptional behaviour. Thats why I would avoid the exception.
Input validation and sanitation is entirely the right thing to do. A program should never crash due to expected values, and expected here means "we know this will happen". The OP has to decide if his program should accept both 6.7 and 6,7 as the same thing or if they mean different things, and if it should mean different things in cultures that uses different decimal points and thousand separators. Since the OP seems to be clueless to these nuances, making him aware of this is also the right thing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.