Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I getting this error. Cannot implicitly convert type 'double' to 'decimal'. An explicit conversation exists (are you missing a cast?) [duplicate]

Tags:

c#

I getting this error. Cannot implicitly convert type 'double' to 'decimal'. An explicit conversation exists (are you missing a cast?) I am a bit rusty on C#. have not done much for about two years

I am to be writing this Write a console application that calculates the amount of income tax required based on filing status and taxable income. The program should prompt the user to enter either a filing status of Single or Married (or Quit). If anything else is entered, an error message should be displayed and the input should be repeated. If the status is either Single or Married, the taxable income is input and the amount of tax is printed. This process should repeat until Quit is entered.

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();
        }
    }
}
like image 735
Matthew Hunt Avatar asked Dec 01 '22 13:12

Matthew Hunt


2 Answers

The compiler treats 0.10 (and any other fractions) as a double by default.

To tell the compiler that it is a decimal, you have to use the m suffix like this:

tax = income * 0.10m;

You have to do this for all numbers used in the equations. For example:

tax = 4867.50m + (35350m - 8700m) * 0.15m; 
like image 100
Yacoub Massad Avatar answered Dec 10 '22 12:12

Yacoub Massad


Simply convert to decimal would not resolve?

tax = Convert.ToDecimal(4867.50 + (70700 - 17400) * 0.15);
like image 41
Victhor Christianno Avatar answered Dec 10 '22 13:12

Victhor Christianno