Link to home
Create AccountLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

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);
            }
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this will work better:
id = id + (decimal).0001;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of kwh3856

ASKER

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

Thanks
Kenny
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

2.000100000003

where the second one would return

2.0001


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

Cheers,

Greg