Decimals bug??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Emilio Dabdoub

    #1

    Decimals bug??

    I'm having a problem doing a simple addition in VB.NET
    ?84.16+39.57
    123.72999999999 999

    it should be: 123.73

    Where is vb.net getting all this decimals?

    Any help will be appreciated.

    Thanks


  • Cor Ligthert

    #2
    Re: Decimals bug??

    Emilio,

    One of the first things if you not are upgrading is setting in the options
    or in the top of your program files.

    Option Strict On

    You will see than that you cannot do what you write, because you are adding
    doubles.

    I hope this helps,

    Cor


    Comment

    • Cor Ligthert

      #3
      Re: Decimals bug??

      Emilio,

      I forgot, please don't write about a bug if you are not sure of that, if
      everybody start telling that it is bug, than Microsoft will sure not take
      notice from that in this newsgroup.

      Thanks in advance.

      Cor


      Comment

      • Larry Lard

        #4
        Re: Decimals bug??


        Emilio Dabdoub wrote:[color=blue]
        > I'm having a problem doing a simple addition in VB.NET
        > ?84.16+39.57
        > 123.72999999999 999
        >
        > it should be: 123.73
        >
        > Where is vb.net getting all this decimals?[/color]

        I've got a better one!

        ?0.1+0.1+0.1-0.3
        0.0000000000000 000555111512312 57827
        [color=blue]
        >
        > Any help will be appreciated.[/color]

        Don't expect exact results if you use floating-point types.

        --
        Larry Lard
        Replies to group please

        Comment

        • Chris Dunaway

          #5
          Re: Decimals bug??

          That is just the nature of using floating point numbers in a binary
          machine. It happens with all languages not just VB or C#. To avoid
          these types of problems, use the Decimal datatype.

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Decimals bug??

            Emilio,
            In addition to the other comments, check out:





            | ?84.16+39.57
            | 123.72999999999 999

            84.16 & 39.57 are doubles, so you are using doubles (binary floating point)
            for the calculation.

            ? CDec(84.16) + CDec(39.57)
            123.73D

            The doubles are converted to Decimals before the addition, so you are using
            Decimal (decimal floating point) for the calculation.

            Instead of CDec(84.16) you could have written 84.16, where 84.16D is a
            Decimal literal, and 84.16 (or 84.16R) is a Double literal, 84.16F happens
            to be a Single literal.

            Hope this helps
            Jay



            "Emilio Dabdoub" <emilio@sandlui s.com> wrote in message
            news:O6LIp0kcFH A.3864@TK2MSFTN GP10.phx.gbl...
            | I'm having a problem doing a simple addition in VB.NET
            | ?84.16+39.57
            | 123.72999999999 999
            |
            | it should be: 123.73
            |
            | Where is vb.net getting all this decimals?
            |
            | Any help will be appreciated.
            |
            | Thanks
            |
            |


            Comment

            Working...