【问题标题】:Multiplying TextBox to a Value [duplicate]将文本框乘以一个值[重复]
【发布时间】:2012-11-11 10:11:22
【问题描述】:

可能重复:
C#, Operator ‘*’ cannot be applied to operands of type ‘double’ and ‘decimal’

您好,我想将插入文本框中的值相乘,但出现错误。这是我的代码。

decimal num1, num2;
if(decimal.TryParse(textBox1.Text, out num1) 
   && decimal.TryParse(textBox2.Text, out num2)){

 decimal ans = num1 * 0.20 + num2 * 0.20;
 Label1.Text = ans.ToString();

        }else{
            MessageBox.Show("Please Put a number!! ");
        }

我在“ans”中有错误,请帮助我。这是我的错误“运算符 * 不能应用于 'decimal' 和 double 类型的操作数;”

【问题讨论】:

标签: c# android-activity


【解决方案1】:

问题在于编译器将常量视为Double
要修复错误本身,您可以像这样将常量转换为十进制:

decimal ans = num1 * (decimal)0.20 + num2 * (decimal)0.20;

或者更好(如 cmets 中所述)您可以 specify the type of the constants

decimal ans = num1 * 0.20m + num2 * 0.20m;

【讨论】:

【解决方案2】:
decimal ans = num1 * 0.20m + num2 * 0.20m;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    相关资源
    最近更新 更多