错误 CS0019:运算符“<=”无法应用于 C# 控制台应用程序中 'double' 和 'decimal' 类型的操作数
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal' in c# console app
我尝试多次编译解决方案。但是当我使用 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();
}
}
我在编译时遇到的错误是:
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '>' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'
但是当我使用 int 或 decimal 而不是 double 作为变量类型时,程序正在编译并且工作正常。
M
表示 decimal
文字。对于双倍,只需使用例如2
(或更好,2d
)而不是 2M
。
2m
是 decimal
因为 m
是十进制的缩写。如果你想要双倍的,你可以使用 d
。
if (priceGain <= 2d)
decimal
和 int
可以在不进行转换的情况下进行比较,但不能像您所经历的那样加倍。
请注意,您可能会遇到浮点数不精确的问题,因此如果可以,请使用小数。
将双精度更改为十进制,它应该可以工作,如下所示:
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();
}
}
我尝试多次编译解决方案。但是当我使用 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();
}
}
我在编译时遇到的错误是:
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '>' cannot be applied to operands of type 'double' and 'decimal'
error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal'
但是当我使用 int 或 decimal 而不是 double 作为变量类型时,程序正在编译并且工作正常。
M
表示 decimal
文字。对于双倍,只需使用例如2
(或更好,2d
)而不是 2M
。
2m
是 decimal
因为 m
是十进制的缩写。如果你想要双倍的,你可以使用 d
。
if (priceGain <= 2d)
decimal
和 int
可以在不进行转换的情况下进行比较,但不能像您所经历的那样加倍。
请注意,您可能会遇到浮点数不精确的问题,因此如果可以,请使用小数。
将双精度更改为十进制,它应该可以工作,如下所示:
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();
}
}