【问题标题】:error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal' in c# console app错误 CS0019:运算符“<=”不能应用于 c# 控制台应用程序中“双精度”和“十进制”类型的操作数
【发布时间】:2016-05-30 15:39:05
【问题描述】:

我多次尝试编译解决方案。但是当我使用 double 作为变量类型时它没有编译。代码是:

using System;
class program
{
    static void Main()
    {
        bool Flag=true;
        string action = "Null";
        double priceGain=0;
        Console.WriteLine("press 'q' or write \"quit\" to exit the application");
        while(Flag==true)
        {
            Console.WriteLine("What is the price Gain? ");
            string input=Console.ReadLine();
            if(double.TryParse(input,out priceGain))
            {
                if (priceGain <= 2m)
                {
                    action ="Sell";
                }
                else if(priceGain > 2m && priceGain <= 3m)
                {
                    action="Do Nothing";
                }
                else
                {
                    action="Buy";
                }
                Console.WriteLine(action);
            }
            else if (input.ToLower()=="q" || input.ToLower() =="quit")
            {
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Invalid Input! please enter a number");
            }
        }
        Console.ReadKey();
    }
}


我在编译时遇到的错误是:

错误 CS0019:运算符“ 错误 CS0019:运算符“>”不能应用于“double”和“decimal”类型的操作数
错误 CS0019:运算符“


但是当我使用 int 或 decimal 而不是 double 作为变量类型时,程序正在编译并且工作正常

【问题讨论】:

  • 错误信息准确地说明了问题所在。双精度不能与十进制比较。所以把它和别的东西比较一下,比如另一个双倍。
  • 如果你不知道简写mdul 等@RaymondChen,那就不太明显了
  • @PatrickHofman 以及 m 后缀在原始程序中,所以我假设他们在编写时知道这意味着什么。
  • 其实我不太习惯使用速记。所以我正在用各种方式练习一个示例程序,并没有识别 m.
  • while (flag == true)代替while (flag)

标签: c# console-application


【解决方案1】:

2mdecimal,因为 m 是十进制的缩写。如果你想要一个双倍的,你可以使用d

if (priceGain <= 2d)

decimalint 可以在没有强制转换的情况下进行比较,但不能像您所经历的那样进行双重比较。

请注意,您可能会受到浮点数不精确的影响,因此如果可以,请使用小数。

【讨论】:

  • 你:decimalint 可以在不强制转换的情况下进行比较,但 double 不能进行比较 更准确地说,decimalint 可以进行比较,同样可以比较doubleint。但这确实是因为存在从intdecimal 以及从intdouble隐式 转换。所以这真的是两个decimal,分别是两个double,经过右边的隐式转换后的比较。所以decimaldouble在这方面是“对称的”,不能直接比较。
【解决方案2】:

M 表示 decimal 文字。对于双倍,只需使用例如2(或者更好,2d)而不是2M

【讨论】:

    【解决方案3】:

    将双精度改为十进制,它应该可以工作,如下所示:

    using System;
    class program
    {
        static void Main()
        {
            bool Flag = true;
            string action = "Null";
            decimal priceGain = 0M;
            Console.WriteLine("press 'q' or write \"quit\" to exit the application");
            while (Flag == true)
            {
                Console.WriteLine("What is the price Gain? ");
                string input = Console.ReadLine();
                if (decimal.TryParse(input, out priceGain))
                {
                    if (priceGain <= 2m)
                    {
                        action = "Sell";
                    }
                    else if (priceGain > 2m && priceGain <= 3m)
                    {
                        action = "Do Nothing";
                    }
                    else
                    {
                        action = "Buy";
                    }
                    Console.WriteLine(action);
                }
                else if (input.ToLower() == "q" || input.ToLower() == "quit")
                {
                    Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("Invalid Input! please enter a number");
                }
            }
            Console.ReadKey();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      相关资源
      最近更新 更多