• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1137
  • Last Modified:

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

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);
1 Solution
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this will work better:
id = id + (decimal).0001;

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
or shorter:
id += (decimal).0001;

Open in new window

kwh3856Author Commented:
your awesome but your title already shows that;)......congrats on that new title.

Kevin CrossChief Technology OfficerCommented:
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

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


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now