473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting problem

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.
Jul 27 '06 #1
7 2662
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.

Jul 27 '06 #2
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);

"Doug" <dn******@dtgnet.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.

Jul 27 '06 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jim Stools" <ji*********@yahoo.comwrote in message
news:ea**********@nntp.aioe.org...
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.


Jul 27 '06 #4
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.

Jul 27 '06 #5
Many Thanks!
"Doug" <dn******@dtgnet.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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.

Jul 27 '06 #6
Jim Stools <ji*********@yahoo.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 27 '06 #7
Doug <dn******@dtgnet.comwrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.