简体   繁体   English

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

[英]C# Operator '*' cannot be applied to operands of type 'double' and 'decimal'

A couple errors I am struggling to understand. 我正在努力理解一些错误。 I am attempting to make a sales tax calculator. 我正在尝试制作营业税计算器。 But keep getting operation error because I cannot multiply a decimal by decimal. 但是由于我无法将十进制乘以十进制,所以会不断出现操作错误。 Here is my code for my calulate button: 这是我的计算按钮的代码:

decimal decTax;
decimal decTotal;
string input;
decimal decOrder;
// Accept the order from the Console window
Console.Write("Enter order in dollars: $");
input = Console.ReadLine();
Decimal.TryParse(input, out decOrder);

// Calculate the sales tax
decTax = (0.06D  * (decOrder + decTax));
decTotal = (decOrder + decTax);
// Write the results to the Console Window
Console.WriteLine();
Console.WriteLine("The Total is " & decTotal.ToString("c"));
Console.ReadLine();

The error is exactly what it says. 该错误正是它所说的。 You can't multiply a decimal by your literal double 0.06D. 您不能将十进制数乘以文字双精度数0.06D。 The reason why you can't do that implicitly is because converting a decimal to a double can result in a lose of precision. 之所以不能隐式执行此操作,是因为将decimal转换为double精度会导致精度下降。 The compiler won't make an automatic conversion if there is a possibility of losing precision. 如果有可能失去精度,编译器将不会进行自动转换。

Try casting it to a decimal too: 也尝试将其转换为小数:

decTax = ((decimal)0.06 * (decOrder + decTax));

Or you can use the m suffix to indicate that your literal is a decimal and not a double: 或者,您可以使用m后缀表示您的文字是十进制而不是双精度:

decTax = 0.06m * (decOrder + decTax));

Note, however, in the code you posted in your question you never assigned an initial value to decTax . 但是请注意,在问题中发布的代码中,您从未为decTax分配初始值。 So decOrder + decTax can't be calculated. 因此无法计算decOrder + decTax It's unclear from your question what you intended decTax to be in that case, but it needs an initial value. 从您的问题尚不清楚在这种情况下您打算将decTax设置为什么,但是它需要一个初始值。

D means Double for Decimal use M . D表示Decimal使用M Double

decTax = (0.06M  * (decOrder + decTax));

Refer to this documentation for more information. 有关更多信息,请参考此文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#,运算符&#39;*&#39;不能应用于&#39;double&#39;和&#39;decimal&#39;类型的操作数 - C#, Operator '*' cannot be applied to operands of type 'double' and 'decimal' C#,运算符 &#39;??&#39; 不能应用于“十进制”和“十进制”类型的操作数 - C#, Operator '??' cannot be applied to operands of type 'decimal' and 'decimal' 运算符'<'不能应用于'decimal'和'double'类型的操作数 - Operator '<' cannot be applied to operands of type 'decimal' and 'double' “运算符&#39;+ =&#39;不能应用于类型&#39;decimal&#39;和&#39;double&#39;的操作数” - “operator '+=' cannot be applied to operands of type 'decimal' and 'double' ” 运算符“ *”不能应用于“ double”和“ decimal”类型的操作数 - Operator '*' cannot be applied to operands of type 'double' and 'decimal' 运算符“ &lt;”不能应用于类型为“ double”和“ decimal”的操作数 - Operator '<' cannot be applied to operands of type 'double' and 'decimal' 错误CS0019:运算符&#39;&lt;=&#39;不能应用于c#控制台应用程序中类型&#39;double&#39;和&#39;decimal&#39;的操作数 - error CS0019: Operator '<=' cannot be applied to operands of type 'double' and 'decimal' in c# console app 奇怪的错误“运算符&#39;*&#39;无法应用于类型&#39;double&#39;和&#39;decimal&#39;的操作数” - weird error “operator '*' cannot be applied to operands of type 'double' and 'decimal'” 运算符&amp;&amp;不能应用于十进制和十进制类型的操作数 - Operator && cannot be applied to operands of type decimal and decimal 运算符&#39;^&#39;不能应用于&#39;double&#39;和&#39;double&#39;类型的操作数 - Operator '^' cannot be applied to operands of type 'double' and 'double'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM