11

When I try to set the z variable in the code below, I get this compile time error:

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

decimal x = 1, y = 2, z;

// There are two ways I set the z variable:
z = (x*y)*(.8 * 1.732050808m);
z = (1000 * x)/(y * 1.732050808m)* .8;

Why is that, and how do I solve it?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
tejas_grande
  • 283
  • 2
  • 7
  • 16

3 Answers3

29

Be sure to use .8m instead of .8.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Jimmy
  • 89,068
  • 17
  • 119
  • 137
4

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.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
3

In this line here:

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

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

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

will fix it.

Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197