converting string from a textbox to a double not working in c#

okay so I'm really confused about this problem and I don't know how to fix it. this is the code and I cant see what I did wrong:

        private void btnFormule_Click(object sender, EventArgs e)
        {

            double NumberA;
            NumberA = double.Parse(txtA.Text);

        }

it also does the same if I do convert.todouble(). But the weird thing is a while ago it didn't give an error when I did it like this so I don't know what's going on.

When I try it out it gives the error "System.FormatException: The format of the input string is incorrect" (its translated so it's not the exact error). if anyone has a solution to this problem it would really help

Upvotes: 0

Views: 306

Answers (1)

Stefano Cavion
Stefano Cavion

Reputation: 741

You should use a combination of double.TryParse and CultureInfo

  private void btnFormule_Click(object sender, EventArgs e)
  {

      if(double.TryParse( textA.Text,NumberStyles.Any, CultureInfo.CurrentCulture, out double NumberA);
      {
        //Manage the valid parsing;
      }
      else
      {
        //Manage the not valid parsing
      }


    }

Upvotes: 1

Related Questions