计算多个文本框时出错

error during calculation multiple textboxes

我发现了一个错误

System.FormatException: 'Input string was not in a correct format.'

在我的 C# 项目中。我想将我的十进制值转换为四舍五入,但如果值为“7.99”,则输出仅为 7,位于 "b"。我使用了这个功能,但我失败了。

void Calculation()
   {
        double a = Convert.ToDouble(lblTFPc.Text); **//Error appears here**
        double b = Convert.ToDouble(lbl1.Text);
        double c = Convert.ToDouble(lblTPc.Text);
        double d = Convert.ToDouble(lblSchOn.Text);
        try
        {

            a = (c / d);

            b = Math.Floor(a +0.0);


        }
        catch (Exception ex)
        {
            MetroFramework.MetroMessageBox.Show(this, "Error" + ex, "Stop", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

我改了方法还是报错

void Calculation()
    {
        try
        {
            decimal a = Convert.ToDecimal(txtValue.Text);
            decimal b = Convert.ToDecimal(lblCtn.Text);
            decimal c = Convert.ToDecimal(lblPc.Text);

            decimal d = Convert.ToDecimal(lblCtnInPc.Text);
            decimal e = Convert.ToDecimal(txtQtyCtn.Text);
            decimal f = Convert.ToDecimal(lblCtnSize.Text);

            decimal g = Convert.ToDecimal(lblTotalPc.Text);
            decimal h = Convert.ToDecimal(txtQtyPc.Text);

            decimal i = Convert.ToDecimal(txtTotalFreePc.Text); **//error is here I change lbl into txtbox but error still**
            decimal j = Convert.ToDecimal(lblSchemeOn.Text);

            decimal k = Convert.ToDecimal(lbl1.Text);

            a = (b + c);
            d = (e * f);
            g = (h + d);
            i = (g / j);
            k = Math.Floor(i);
        }
        catch (Exception ex)
        {
            MetroFramework.MetroMessageBox.Show(this, "Error" + ex, "Stop", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

尝试错误是这样的:

ErrorSystem.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String Str, NumberStryles options, NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseDecimal(String value, NumberStyles options, NUmberFormatInfo numfmt) atSystem.Convert.ToDecimal(String value) at MyProject.Forms.frmPurchaseItem.Calculation() in [Filepath]

您是否尝试过在文本框中使用“0,55”值而不是“0.55”。或者你也可以使用 TryParse 函数。

double result=0;
if(Double.TryParse(lblTFPc.Text, out result))
...

lbl 是标签的控件缩写,文本框是 txt。检查文本 属性 中的标签是否有文本而不是数字。也许您放置标签而不是文本框。 Convert.ToDouble()如果有文字则无法进行转换