Casting problem

  • Thread starter Thread starter Jim Stools
  • Start date Start date
J

Jim Stools

This does not work --- why? How do you cast a simple formula?

Int32 d = 34.12345;
decimal c = 10.25;
decimal amount = 0.00;

amount = (decimal) (c * d);


Thanks In Advance.
 
You have a non-integer value in your Int32 variable which will prevent
your code from even compiling. If you need an Int32 variable you will
need to remove everything after the decimal point. If you don't need
an Int32 variable just switch it to a decimal and you should be fine
(and then you don't need to cast it in your last line like you are
doing).

Also, DotNet won't compile this because it can't translate 10.25 or
0.00 into a decimal value unless you put an M after them (i.e. 10.25M
and 0.00M) This is because it is assuming those values are doubles and
it won't allow you to convert them implicitly to a decimal like that.
 
Very Sorry, my typing out ran my brain... I meant the Int32 to be a double.
Whay doesn't it work?

double d = 34.12345;
decimal c = 10.25;
decimal amount = 0.00;

amount = (decimal) (c * d);
 
Jim,

The problem here is twofold.

First, you can not implicitly convert a double (34.12345) to an int, so
you have to explicitly cast it, like so:

int d = (int) 34.12345;

Second, double amounts are not implicitly convertable to decimals. So
you have to either explicitly cast them, or declare them with "M" after to
indicate they are decimals, like so:

decimal c = 10.25M;
decimal amount = 0.00M;

Hope this helps.
 
You'd have to put an M behind 10.25 and 0.00 in this situation too.
But, your other problem is that DotNet doesn't allow you to multiple a
decimal and a double together (as to why, I'm not sure). If you have
to have that one value as a double for other reasons you could get
around your problem like this:

double d = 34.12345;
decimal c = 10.25M;
decimal amount = 0.00M;

amount = (c * Convert.ToDecimal(d));

I think that should get you what you are looking for.
 
Jim Stools said:
This does not work --- why? How do you cast a simple formula?

Int32 d = 34.12345;
decimal c = 10.25;
decimal amount = 0.00;

amount = (decimal) (c * d);

Well, for a start it doesn't work because
Int32 d = 34.12345;
won't compile - 34.12345 is not an integer.

Next it won't compile because 10.25 is a double, not a decimal.
Likewise 0.00.

Finally, it won't compile because you can't multiply a double by a
decimal. Assuming you meant d to be a double instead of an int, you end
up with double*decimal in the last line. Because of the differences in
range and precision, .NET doesn't let you do that multiplication -
you'll need to cast one of them to the type of the other for it to
work.
 
Doug said:
You'd have to put an M behind 10.25 and 0.00 in this situation too.
But, your other problem is that DotNet doesn't allow you to multiple a
decimal and a double together (as to why, I'm not sure).

Here's the reason, from the C# 2.0 spec:

<quote>
The decimal type has greater precision but may have a smaller range
than the floating-point types. Thus, conversions from the floating-
point types to decimal might produce overflow exceptions, and
conversions from decimal to the floating-point types might cause loss
of precision or overflow exceptions. For these reasons, no implicit
conversions exist between the floating-point types and decimal, and
without explicit casts, a compile-time error occurs when floating-point
and decimal operands are directly mixed in the same expression.
</quote>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top