【问题标题】:Unhandled String in incorrect Format [duplicate]格式不正确的未处理字符串[重复]
【发布时间】:2019-04-06 22:13:49
【问题描述】:

基本刷新了一些制作转换器的C#知识;我试图在textbox 中没有数字时弹出一个窗口,但是当我单击button 时出现此错误:

System.FormatException: '输入字符串的格式不正确。'

private void btnConvert_Click(object sender, EventArgs e)
    {

        // Killogram convertion

        double i = double.Parse(txtboxAmount.Text);
        if (comboBoxFrom.SelectedItem == "KG" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i * 2.20462262185;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.2046;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i * 6.35;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        // Pound convertion


        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "ST")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.205;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        // Stone convertion

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i * 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "ST")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.205;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        else if (comboBoxFrom.SelectedItem == null && comboBoxTo.SelectedItem == null)
        {
            MessageBox.Show("Enter a valid weight amount and/or select a unit of measurement.");
        }

    }

导致问题的行在下面我把它变成一个带有 to string 但没有运气的字符串

double i = double.Parse(txtboxAmount.Text);

【问题讨论】:

  • 使用double.TryParse() 获得额外的好运。

标签: c# string casting


【解决方案1】:

使用TryParse 优雅地失败:

var text = "123.4";

double result;

if (double.TryParse(text, out result))
{
    // do something
}

如果您使用法语系统并输入英文符号,则要考虑的另一件事可能是文化,

. 而不是 , 它将无法解析您的输入。

if (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out result))
{
    // do something
}

【讨论】:

  • 我曾想过使用 TryParse,但结果是什么??
  • 我应该尝试不同的方法来解决这个问题吗?
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2018-09-23
相关资源
最近更新 更多