Trouble adding two double values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahsuman
    New Member
    • Jun 2007
    • 2

    Trouble adding two double values

    Hai ,

    This is a bit weird for me.It may be that i got it wrong, but i request you to respond please.

    All i need to do is add two double values

    Dim a as double =324.0
    Dim b as double=0.1
    Dim c ad double

    c=a+b


    i expected the output to be c= 324.1 but i am getting 324.1015625, i tried using decimal datatype also but same problem.

    i am inserting c value in sql server 2005 db there i am getting c value as 324.1015625, but in the code i am getting 324.1, why is this?,the datatype field for c in db is float.


    please give me the vb.net code handle this.
  • chinu
    New Member
    • Jun 2007
    • 36

    #2
    Originally posted by ahsuman
    Hai ,

    This is a bit weird for me.It may be that i got it wrong, but i request you to respond please.

    All i need to do is add two double values

    Dim a as double =324.0
    Dim b as double=0.1
    Dim c ad double

    c=a+b


    i expected the output to be c= 324.1 but i am getting 324.1015625, i tried using decimal datatype also but same problem.

    i am inserting c value in sql server 2005 db there i am getting c value as 324.1015625, but in the code i am getting 324.1, why is this?,the datatype field for c in db is float.


    please give me the vb.net code handle this.
    Its quite suprising to see this as i dont think anyone will get other than 324.1 as I also tried in both C# and VB.net. can u delete the pdb files and open the project once again and try.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      I believe this is due to decimal rounding on the server.

      When I do the following:
      [code=sql]
      DECLARE @a AS FLOAT
      SET @a = 324.1
      SELECT @a
      [/code]
      I recieve 324.10000000000 2 as my result.

      If I were to venture a guess, it would go something like this: SQL Server is going to try and store the data in such a way that the value you INSERT and the value you SELECT will be the same value. There are tricks and hacks in the driver to get this to happen, and this is what you are seeing here.

      Comment

      • ahsuman
        New Member
        • Jun 2007
        • 2

        #4
        i still have the same problem please help , i tried using decimal datatype in .net but i am getting the same result

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by ahsuman
          i still have the same problem please help , i tried using decimal datatype in .net but i am getting the same result
          What amount of precision are you aiming for? Can't you use one of the rounding functions to get what you are looking for?

          Comment

          • qkhader
            New Member
            • Jun 2007
            • 1

            #6
            hi
            i am having the same problem, but a work around this is

            double a =2.1
            double b = 0.1
            double c = System.Convert. ToDouble(( a+b).ToString() );

            i know this is not a good solution but will do tell i can find something else, please update as well if you have a better solution

            thanks

            Comment

            Working...