C#,运算符“*”不能应用于“double”和“decimal”类型的操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/363706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:38:17  来源:igfitidea点击:

C#, Operator '*' cannot be applied to operands of type 'double' and 'decimal'

c#decimalmultiplying

提问by tejas_grande

This error should be a simple one but I cant seem to make it work. The problem lies in the fact that this very same code works earlier in the program. I don's see any reason for it to be sending an error on this instance and not the four previous ones. Reference the code below, and feel free to provide any criticism you may have as it should make me better. If it matters, I am using Sharp Develop 2.2.

这个错误应该是一个简单的错误,但我似乎无法让它工作。问题在于这个完全相同的代码在程序的早期工作。我看不出有任何理由在此实例上发送错误而不是前四个错误。参考下面的代码,并随时提供您可能有的任何批评,因为它应该让我变得更好。如果重要的话,我正在使用 Sharp Develop 2.2。

Here is an example of the code that works:

这是一个有效的代码示例:

void calc2Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_e.Text))
    {
        MessageBox.Show("Enter either kVA and Voltage or FLA and Voltage", "Invalid Data Entry", MessageBoxButtons.OK);
    }       

        if (!String.IsNullOrEmpty(tb2_kva.Text) & !String.IsNullOrEmpty(tb2_e.Text))
    { 
            decimal x, y, z;
            x = decimal.Parse(tb2_kva.Text);      
            y = decimal.Parse(tb2_e.Text);
            z = (x * 1000) / (1.732050808m * y); //the m at the end of the decimal allows for the multiplication of decimals    
            tb2_fla.Text = z.ToString();
            tb2_fla.Text = Math.Round(z,2).ToString();
    }
        else
    {
        if (!String.IsNullOrEmpty(tb2_fla.Text) & !String.IsNullOrEmpty(tb2_e.Text))
    { 
            decimal x, y, z;
            x = decimal.Parse(tb2_fla.Text);      
            y = decimal.Parse(tb2_e.Text);
            z = (x * y * 1.732050808m) / 1000; //the m at the end of the decimal allows for the multiplication of decimals  
            tb2_kva.Text = Math.Round(z,2).ToString();

    }

Here is the example of the code that sends the error in the subject line of this post:

这是在这篇文章的主题行中发送错误的代码示例:

void Calc4Click(object sender, EventArgs e)
{
        if (!String.IsNullOrEmpty(tb4_fla.Text) && String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_kw.Text) & String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_e.Text))
        {   //If values are entered improperly, the following message box will appear
        MessageBox.Show("Enter either FLA and Voltage or kW and Voltage", "Invalid Data Entry", MessageBoxButtons.OK);
        }   


        if (!String.IsNullOrEmpty(tb4_fla.Text)&& !String.IsNullOrEmpty(tb4_e.Text)&& String.IsNullOrEmpty(tb4_kw.Text))
        {//If the user eneters FLA and Voltage calculate for kW

            decimal x, y, z;
            x = decimal.Parse(tb4_fla.Text);
            y = decimal.Parse(tb4_e.Text);
            z = (x*y)*(.8 * 1.732050808m);
            tb4_kw.Text = Math.Round(z,0).ToString();

        }               

        if (!String.IsNullOrEmpty(tb4_kw.Text) && !String.IsNullOrEmpty(tb4_e.Text) && String.IsNullOrEmpty(tb4_fla.Text))
        {;//If the user enters kW and Voltage calculate for FLA
            decimal x, y, z;
            x = decimal.Parse(tb4_kw.Text);
            y = decimal.Parse(tb4_e.Text);
            z = (1000 * x)/(y * 1.732050808m)* .8;
            tb4_fla.Text = Math.Round(z,0).ToString();
        }

    }

I appreciate any help that I can get. Thank you.

我很感激我能得到的任何帮助。谢谢你。

采纳答案by Jimmy

.8m instead of .8

回答by Dylan Beattie

In this line here:

在此行中:

z = (xy)(.8 * 1.732050808m);

z = (x y)(.8 * 1.732050808m);

you specify .8 as a literal, but without the 'm' suffix, the literal specifies a double.

您将 .8 指定为文字,但没有 'm' 后缀,文字指定双精度值。

z = (xy)(.8m * 1.732050808m);

z = (x y)(.8m * 1.732050808m);

will fix it.

将修复它。

回答by Harper Shelby

You didn't say which line it was, but I'm betting on these two:

你没有说那是哪条线,但我打赌这两条:

z = (x*y)*(.8 * 1.732050808m);

And:

和:

z = (1000 * x)/(y * 1.732050808m)* .8;

Note that your .8 does not have the 'm' qualifier. Every other place I see you did supply that.

请注意,您的 .8 没有“m”限定符。我看到的每个其他地方都提供了那个。