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

Posted on 2008-11-06
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 142

    Expert Comment

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

    Open in new window

    LVL 142

    Accepted Solution

    or shorter:
    id += (decimal).0001;

    Open in new window


    Author Closing Comment

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

    LVL 59

    Expert Comment

    by:Kevin Cross
    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

    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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now