Error 2 Operator '+' cannot be applied to operands of type 'decimal' and 'double'

Posted on 2008-11-06
Medium Priority
Last Modified: 2013-12-17
Not sure what this means.  Can someone point me in the right direction?

Here is my code:

            if (reader.HasRows)
                id = Convert.ToDecimal(reader["id"].ToString());

                id = id + .0001;-----------------------------------------------------------Error Occurs Here
                Session.Add("id", id);
Question by:kwh3856
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22895189
this will work better:
id = id + (decimal).0001;

Open in new window

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22895193
or shorter:
id += (decimal).0001;

Open in new window


Author Closing Comment

ID: 31513908
your awesome but your title already shows that;)......congrats on that new title.

LVL 60

Expert Comment

by:Kevin Cross
ID: 22895313
Angel's answer is the right one, but in the spirit of making shorter, you can do this as well.

id += .0001m;

Open in new window

LVL 37

Expert Comment

ID: 22898251
actually mwvisa1 your answer is correct and here is why ....

id = id + (decimal) .0001;

says ...

id is equal to id plus the conversion of the double value .0001 to a decimal.

id += .0001m; or id = id + .0001m;

says ...

id is equal to id plus .0001

While these two may SEEM to be equivalent they are not because of how floating point math works on computers. Since floating point numbers are handled by approximations ... on some machines (or some JIT implementations) you may end up with a scenario where the first bit of code for an id of 1 returns


where the second one would return


For more details I would suggest reading http://docs.sun.com/source/806-3568/ncg_goldberg.html



Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question