简体   繁体   English

将字符串从文本框转换为 double 在 c# 中不起作用

[英]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().如果我执行 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).当我尝试它时,它给出了错误“System.FormatException:输入字符串的格式不正确”(它的翻译因此不是确切的错误)。 if anyone has a solution to this problem it would really help如果有人对此问题有解决方案,那真的会有所帮助

You should use a combination of double.TryParse and CultureInfo您应该使用double.TryParseCultureInfo的组合

  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
      }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM