c# – mscorlib.dll中出现未处理的“System.FormatException”类型异常

前端之家收集整理的这篇文章主要介绍了c# – mscorlib.dll中出现未处理的“System.FormatException”类型异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每次我点击计算按钮时,我收到以下消息:mscorlib.dll中发生未处理的“System.FormatException”类型异常
附加信息:输入字符串格式不正确.

当工资输入低于9.75美元时,我应该显示一条消息.

然后它突出显示这行代码

if (Convert.ToInt32(RateTextBox.Text) < 9.75m)

这是Calculate Button方法(我确定我犯了多个错误):

private void CalcButton_Click(object sender,EventArgs e)
    {  // The “Calculate” button calculates gross pay,taxes,and net pay and then      displays name,department,gross pay,and net pay using currency format for varIoUs amounts in the rich text Box
        // Gross pay=  (hours * rate)
        // Taxes= (25% of gross pay)
        // Net pay (gross pay ?taxes)



        //calculate         


        Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text);
        Taxes = TAX * Gross_pay;
        Net_Pay = Gross_pay - Taxes;

        annual_salary = Net_Pay;



        //display
        DisplayOutPut.Text = "";
        DisplayOutPut.Text += NameTextBox.Text + "\n";
        DisplayOutPut.Text += "Hours:" + HoursTextBox.Text + "\n";
        DisplayOutPut.Text += "Rate:" + RateTextBox.Text + "\n";
        DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n"; // Hours*Rate
        DisplayOutPut.Text += "Taxes:" + Taxes.ToString("C") + "\n";
        DisplayOutPut.Text += "Net Pay:" + Net_Pay.ToString("C");

        //handling the invalid inputs
        if (NameTextBox.Text == "")
        { MessageBox.Show("Name is missing.","Error"); }

        if (Convert.ToInt32(HoursTextBox.Text) >= 70)
        { MessageBox.Show("Please Enter a Valid hour.","Invalid data type."); }

        if (RateTextBox.Text == "" && (RateTextBox.Text == ","))
        { MessageBox.Show("Please Enter a valid amount.","Invalid data type ($)"); }

        if (Convert.ToInt32(HoursTextBox.Text) >= 70)
        { MessageBox.Show("You have exceeded the maximum hours per week."); }

        else if (Convert.ToInt32(HoursTextBox.Text) < 10)
        { MessageBox.Show("You cannot input less than 10 hours."); }

        if (Convert.ToInt32(RateTextBox.Text) < 9.75m)
        { MessageBox.Show("Please enter the minimum wage."); }


        //overtime pay
        if (Convert.ToInt32(HoursTextBox.Text) >= 41)
        {
            Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) * 1.5m;
            DisplayOutPut.Text += "Gross Pay:" + Gross_pay.ToString("C") + "\n";
        }

        //Medical/Dental and 401k deductions...as well as tax collected.
        if (MedicalDentalDeductions.Checked)
        {
            Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 50.00m;
        }

    if(FourOneKDeduction.Checked)
    {
        Gross_pay = Convert.ToInt32(HoursTextBox.Text) * decimal.Parse(RateTextBox.Text) - 0.05m * 100;


    }


    if ((MedicalDentalDeductions.Checked) && (FourOneKDeduction.Checked))
    { Taxes = TAX * Gross_pay; }
      DisplayOutPut.Text= "Medical/Dental deduction:" + Taxes +"401k deduction:"+ Taxes;

    }

解决方法

您正在将数字转换为整数.所以如果你输入(比如说)9.5会失败,因为它不是一个整数.

您几乎肯定会使用decimal.TryParse,注意返回值将说明解析是否成功.

decimal userRate;
if (!decimal.TryParse(RateTextBox.Text,out userRate))
{
    // Indicate to the user that the input is invalid,and return from
    // the method
}
// Now use userRate
原文链接:https://www.f2er.com/csharp/99604.html

猜你在找的C#相关文章