我收到这个错误。无法将类型'double'隐式转换为'decimal'。存在明确的对话(你是否错过了演员?)

时间:2016-01-20 21:23:50

标签: c#

我收到此错误。无法将类型'double'隐式转换为'decimal'。存在明确的对话(你是否错过了演员?)我在C#上有点生疏。大约两年没做多少

我要写这篇文章 编写一个控制台应用程序,根据归档状态计算所需的所得税金额 应纳税所得额。 该程序应提示用户输入单身或已婚(或退出)的申请状态。如果 输入任何其他内容时,应显示错误消息并重复输入。 如果状态为单身或已婚,则输入应纳税所得额并打印税额。 此过程应重复,直到输入Quit。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void gobutton_Click(object sender, EventArgs e)
        {
            decimal income  //hold income value
            decimal tax     //hold tax value  

            //get income value
            income = int.Parse(textBox2.Text);


            //Display the output ino the tax output label
            taxoutputlabel.Text = tax;

            if (textBox1.Text == "single")
                if (income <= 8700)
                    tax = income * 0.10;
                else if (income <= 35350)
                    tax = 4867.50 + (35350 - 8700) * 0.15;
                else if (income <= 85650)
                    tax = 17442.50 + (85650 - 35350) * 0.25;
                else if (income <= 178650)
                    tax = 43482.50 + (178650 - 85650) * 0.28;
                else if (income <= 388350)
                    tax = 112683.50 + (388350 - 178650) * 0.35;
            else if (textBox1.Text == "marriage, married")
                if (income <= 17400)
                        tax = income * 0.10;
                else if (income <= 70700)
                        tax = 4867.50 + (70700 - 17400) * 0.15;
                else if (income <= 142700)
                        tax = 17442.50 + (142700 - 70700) * 0.25;
                else if (income <= 217450)
                        tax = 43482.50 + (217450 -142700) * 0.28;
                else if (income <= 388350)
                        tax = 112683.50 + (388350 - 178650) * 0.35;
            else if (textBox1.Text == "divorced")
                if (income <= 8700)
                    tax = income * 0.10;
                else if (income <= 35350)
                    tax = 4867.50 + (35350 - 8700) * 0.15;
                else if (income <= 85650)
                    tax = 17442.50 + (85650 - 35350) * 0.25;
                else if (income <= 178650)
                    tax = 43482.50 + (178650 - 85650) * 0.28;
                else if (income <= 388350)
                    tax = 112683.50 + (388350 - 178650) * 0.35;

        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            //closes form
            this.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:7)

编译器默认将0.10(和任何其他分数)视为double

要告诉编译器它是小数,你必须使用这样的m后缀:

tax = income * 0.10m;

您必须对方程中使用的所有数字执行此操作。例如:

tax = 4867.50m + (35350m - 8700m) * 0.15m; 

答案 1 :(得分:3)

简单地转换为十进制无法解决?

tax = Convert.ToDecimal(4867.50 + (70700 - 17400) * 0.15);