0

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 1
    I suggest to use `double.TryParse` instead of `double.Parse` since not every string could be converted to a double, also you should take care about the current culture when parsing see [here](https://stackoverflow.com/questions/5109816/double-parse-with-culture-format) – Stefano Cavion Feb 07 '20 at 11:14
  • As per the docs for [`double.Parse()`](https://learn.microsoft.com/en-us/dotnet/api/system.double.parse?view=netframework-4.8#System_Double_Parse_System_String_) you'll get a FormatException if the string you're passing does not represent a valid format – Ian Feb 07 '20 at 11:16
  • Check if it's a locale issue. Probably the decimal separator you're using is different from what the system expects. Use [`double.Parse(string s, IFormatProvider provider)`](https://learn.microsoft.com/en-us/dotnet/api/system.double.parse?view=netframework-4.8#System_Double_Parse_System_String_System_IFormatProvider_) – Magnetron Feb 07 '20 at 11:17
  • I like using `Convert` class and just wrap it into `try/catch` so function from it you need would be `Convert.ToDouble(object)` – Aleksa Ristic Feb 07 '20 at 11:18
  • @AleksaRistic Convert is nothing more than [a wrapper for `double.Parse`](https://referencesource.microsoft.com/#mscorlib/system/convert.cs,1600). It fails exactly the same way if the format is wrong. It accepts a CultureInfo too. – Panagiotis Kanavos Feb 07 '20 at 11:29
  • `I don't know what's going on.` you changed your account's or server's locale. Or a user tried to enter US-style numbers when the locale is set to eg France or Belgium, etc. All parsing and formatting methods use the thread's CurrentCulture to parse or format. The defaulte Culture is the same as the user's locale. – Panagiotis Kanavos Feb 07 '20 at 11:33
  • What type of application are you building? Desktop? Web? Do you have to handle strings that *aren't* in the user's locale? Or perhaps, do you have to handle *multiple* locales at the same time? – Panagiotis Kanavos Feb 07 '20 at 11:37

1 Answers1

1

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
      }


    }
Stefano Cavion
  • 641
  • 8
  • 16
  • `CultureInfo.CurrentCulture` is the *default* – Panagiotis Kanavos Feb 07 '20 at 11:31
  • you are right but here I don't know which is the problem with that string since the op didn't post it. I could suppose that the problem is on the decimal/thousand separator since this is different in many Culture. Of course he can set the culture to use instead of the current culture – Stefano Cavion Feb 07 '20 at 11:41