Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How come I get a rounded number when I multiply doubles?? [modified]

How come I get a rounded number when I multiply doubles?? [modified]

Scheduled Pinned Locked Moved C#
helpquestion
22 Posts 15 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R roman_s

    How come I get a rounded number when I multiply and assign value to a double??

    dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

    double dbRecipeTotal = 0.0;
    RI.Quantity is = 28.88887
    dbRecipeTotalQuantity is = 28.88887
    InvItem.Price is = 2425

    I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

    modified on Wednesday, July 28, 2010 9:44 AM

    T Offline
    T Offline
    T M Gray
    wrote on last edited by
    #5

    Whenever you do math between two number types you should expect your answer will be in precision of the lowest precision operand. You should either declare the 100 as a constant of the appropriate type or use Numeric type suffixes[^].

    1 Reply Last reply
    0
    • R roman_s

      How come I get a rounded number when I multiply and assign value to a double??

      dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

      double dbRecipeTotal = 0.0;
      RI.Quantity is = 28.88887
      dbRecipeTotalQuantity is = 28.88887
      InvItem.Price is = 2425

      I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

      modified on Wednesday, July 28, 2010 9:44 AM

      W Offline
      W Offline
      William Winner
      wrote on last edited by
      #6

      Try making 100 100.0 instead. It's taking price and dividing by an integer which forces the result of the operation to an integer. Telling it that 100 isn't an integer by adding the decimal point should give you the right answer.

      T 1 Reply Last reply
      0
      • W William Winner

        Try making 100 100.0 instead. It's taking price and dividing by an integer which forces the result of the operation to an integer. Telling it that 100 isn't an integer by adding the decimal point should give you the right answer.

        T Offline
        T Offline
        T M Gray
        wrote on last edited by
        #7

        Just adding a .0 isn't good practice. Do you know for sure what type it becomes by adding a .0? It could be a decimal, or a float, or a double. Even if you know what type the compiler chooses by default, will other people who have to read or update your code? See my previous post in the thread for how to make sure the number you are dealing with is cast as exactly the type you need.

        T 1 Reply Last reply
        0
        • R roman_s

          How come I get a rounded number when I multiply and assign value to a double??

          dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

          double dbRecipeTotal = 0.0;
          RI.Quantity is = 28.88887
          dbRecipeTotalQuantity is = 28.88887
          InvItem.Price is = 2425

          I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

          modified on Wednesday, July 28, 2010 9:44 AM

          P Offline
          P Offline
          pinskerr
          wrote on last edited by
          #8

          Your other respondents are right that InvItem.Price appears to be an integer, so you would expect the answer to be 24.0. However you say the answer is 12.0, that is really weird - is that what you meant?

          R 1 Reply Last reply
          0
          • P pinskerr

            Your other respondents are right that InvItem.Price appears to be an integer, so you would expect the answer to be 24.0. However you say the answer is 12.0, that is really weird - is that what you meant?

            R Offline
            R Offline
            roman_s
            wrote on last edited by
            #9

            Sorry there is a mathematical mistake what I needed to know is when dividing an integer with a remainder I was getting double variable(2425) / 100 would give you 24.0 , I was wondering why I wasn't getting 24.25 solution was because I was dividing an integer into another integer, I corrected this simply by multiplying 100.00 instead of 100

            1 Reply Last reply
            0
            • R roman_s

              How come I get a rounded number when I multiply and assign value to a double??

              dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

              double dbRecipeTotal = 0.0;
              RI.Quantity is = 28.88887
              dbRecipeTotalQuantity is = 28.88887
              InvItem.Price is = 2425

              I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

              modified on Wednesday, July 28, 2010 9:44 AM

              E Offline
              E Offline
              ely_bob
              wrote on last edited by
              #10

              I've run into this problem Many times... You need to explicitly tell the compiler what type you want from each operation

              dbRecipeTotal = (double)(((double)(RI.Quantity / dbRecipeTotalQuantity)) * ((double)(InvItem.Price / 100)));

              this will make sure that you are always in the precision your looking for, indepentant of whether you use a lower precision numeral type.

              I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

              C 1 Reply Last reply
              0
              • E ely_bob

                I've run into this problem Many times... You need to explicitly tell the compiler what type you want from each operation

                dbRecipeTotal = (double)(((double)(RI.Quantity / dbRecipeTotalQuantity)) * ((double)(InvItem.Price / 100)));

                this will make sure that you are always in the precision your looking for, indepentant of whether you use a lower precision numeral type.

                I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

                C Offline
                C Offline
                Charvak Karpe
                wrote on last edited by
                #11

                ely_bob wrote:

                dbRecipeTotal = (double)(((double)(RI.Quantity / dbRecipeTotalQuantity)) * ((double)(InvItem.Price / 100))); this will make sure that you are always in the precision your looking for, indepentant of whether you use a lower precision numeral type.

                ely_bob, your solution still doesn't work because (InvItem.Price / 100) will be 24, so ((double)(InvItem.Price / 100)) will be 24.0 It needs to be ((double)InvItem.Price / 100). I didn't cast the 100 to double because when one of the arguments to operator/ is a double, the other one is automatically cast (I think).

                E U 2 Replies Last reply
                0
                • T T M Gray

                  Just adding a .0 isn't good practice. Do you know for sure what type it becomes by adding a .0? It could be a decimal, or a float, or a double. Even if you know what type the compiler chooses by default, will other people who have to read or update your code? See my previous post in the thread for how to make sure the number you are dealing with is cast as exactly the type you need.

                  T Offline
                  T Offline
                  Theraot
                  wrote on last edited by
                  #12

                  According to ECMA-334 9.4.4.3 Real literals Paragraph 2: "If no real-type-suffix is specified, the type of the real literal is double."* *: in ECMA-334 9.4.4.3 a grammar form that excludes any number without a real-type-suffix or decimal-digits after the dot to be a real literal. If you are on a non ECMA compilant implementation of C#, move to another compiler/interpreter.

                  1 Reply Last reply
                  0
                  • C Charvak Karpe

                    ely_bob wrote:

                    dbRecipeTotal = (double)(((double)(RI.Quantity / dbRecipeTotalQuantity)) * ((double)(InvItem.Price / 100))); this will make sure that you are always in the precision your looking for, indepentant of whether you use a lower precision numeral type.

                    ely_bob, your solution still doesn't work because (InvItem.Price / 100) will be 24, so ((double)(InvItem.Price / 100)) will be 24.0 It needs to be ((double)InvItem.Price / 100). I didn't cast the 100 to double because when one of the arguments to operator/ is a double, the other one is automatically cast (I think).

                    E Offline
                    E Offline
                    ely_bob
                    wrote on last edited by
                    #13

                    Yeah your right.. you need to be more liberal with your (double)'s.... That just goes to show you.. don't post before coffee.... :laugh:

                    I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

                    1 Reply Last reply
                    0
                    • R roman_s

                      How come I get a rounded number when I multiply and assign value to a double??

                      dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

                      double dbRecipeTotal = 0.0;
                      RI.Quantity is = 28.88887
                      dbRecipeTotalQuantity is = 28.88887
                      InvItem.Price is = 2425

                      I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

                      modified on Wednesday, July 28, 2010 9:44 AM

                      K Offline
                      K Offline
                      Kenneth Kasajian
                      wrote on last edited by
                      #14

                      A floating point number can never be specified exactly in memory, unless the number is an even power of two. If you just ignore the division part of your example. Even if you were to type: Even if you were type: dbRecipeTotal = 24.25; It may display as 24.25 or 24.0 or 24.24999999999999 depending on how print formating is specified. Just remember, you're never going to store 24.25 in a floating point number exactly.

                      ken@kasajian.com / www.kasajian.com

                      P 1 Reply Last reply
                      0
                      • K Kenneth Kasajian

                        A floating point number can never be specified exactly in memory, unless the number is an even power of two. If you just ignore the division part of your example. Even if you were to type: Even if you were type: dbRecipeTotal = 24.25; It may display as 24.25 or 24.0 or 24.24999999999999 depending on how print formating is specified. Just remember, you're never going to store 24.25 in a floating point number exactly.

                        ken@kasajian.com / www.kasajian.com

                        P Offline
                        P Offline
                        PaulLinton
                        wrote on last edited by
                        #15

                        You are always going to store 24.25 in a floating point number exactly. 24.25 is a power of two. You should never see any of the results you listed (except 24.25) no matter what print formatting is specified.

                        K M 2 Replies Last reply
                        0
                        • P PaulLinton

                          You are always going to store 24.25 in a floating point number exactly. 24.25 is a power of two. You should never see any of the results you listed (except 24.25) no matter what print formatting is specified.

                          K Offline
                          K Offline
                          Kenneth Kasajian
                          wrote on last edited by
                          #16

                          Thanks. I wasn't aware that 24.25 is an even power of two. Learn something new every day.

                          ken@kasajian.com / www.kasajian.com

                          1 Reply Last reply
                          0
                          • R roman_s

                            How come I get a rounded number when I multiply and assign value to a double??

                            dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

                            double dbRecipeTotal = 0.0;
                            RI.Quantity is = 28.88887
                            dbRecipeTotalQuantity is = 28.88887
                            InvItem.Price is = 2425

                            I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

                            modified on Wednesday, July 28, 2010 9:44 AM

                            S Offline
                            S Offline
                            sushantpokharel
                            wrote on last edited by
                            #17

                            The answer lies in the way implicit data type conversion works. Dividing and integer by and integer will never yeild a float or a double. So, InvItem.Price / 100 becomes 24 and not 24.25. I'm sure you understand the rest. To get correct answer do something like

                            ( InvItem.Price / 100.0 ) * ( RI.Quantity / dbRecipeTotalQuantity )

                            1 Reply Last reply
                            0
                            • P PaulLinton

                              You are always going to store 24.25 in a floating point number exactly. 24.25 is a power of two. You should never see any of the results you listed (except 24.25) no matter what print formatting is specified.

                              M Offline
                              M Offline
                              Mitch Hughes
                              wrote on last edited by
                              #18

                              [Stepping on my soapbox] When dealing with floating point numbers, you should never assume that a number will be stored exactly. While some numbers can be stored exactly, most will not be. From the cases I have encountered, too many people assume that a floating point number is good enough for exact numerical needs. But if the value 24.24999999999999 instead of 24.25 would be considered incorrect in your application, you should not be using floating point values. Time and time again I have been called in to analyze why dollar values were coming in off by a penny or two (or more), and it has always* boiled down to the choice of using floating point representation to store exact values. * Since 1992 I have encountered only 2 instances where the problem was an actual bug in the library.

                              1 Reply Last reply
                              0
                              • R roman_s

                                How come I get a rounded number when I multiply and assign value to a double??

                                dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

                                double dbRecipeTotal = 0.0;
                                RI.Quantity is = 28.88887
                                dbRecipeTotalQuantity is = 28.88887
                                InvItem.Price is = 2425

                                I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

                                modified on Wednesday, July 28, 2010 9:44 AM

                                U Offline
                                U Offline
                                User 3150960
                                wrote on last edited by
                                #19

                                I think InvItem.Price is int so 2425/100=24 you can write : InvItem.Price/100.0 to get correct result

                                1 Reply Last reply
                                0
                                • R roman_s

                                  How come I get a rounded number when I multiply and assign value to a double??

                                  dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

                                  double dbRecipeTotal = 0.0;
                                  RI.Quantity is = 28.88887
                                  dbRecipeTotalQuantity is = 28.88887
                                  InvItem.Price is = 2425

                                  I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

                                  modified on Wednesday, July 28, 2010 9:44 AM

                                  U Offline
                                  U Offline
                                  User 3150960
                                  wrote on last edited by
                                  #20

                                  I think InvItem.Price is int so 2425/100=24 you can write : InvItem.Price/100.0 to get correct result: (this is tested and returns correct result): double Quantity = 28.88887; double dbRecipeTotalQuantity = 28.88887; int Price = 2425; double dbRecipeTotal = (Quantity / dbRecipeTotalQuantity) * (Price / 100.0);

                                  1 Reply Last reply
                                  0
                                  • C Charvak Karpe

                                    ely_bob wrote:

                                    dbRecipeTotal = (double)(((double)(RI.Quantity / dbRecipeTotalQuantity)) * ((double)(InvItem.Price / 100))); this will make sure that you are always in the precision your looking for, indepentant of whether you use a lower precision numeral type.

                                    ely_bob, your solution still doesn't work because (InvItem.Price / 100) will be 24, so ((double)(InvItem.Price / 100)) will be 24.0 It needs to be ((double)InvItem.Price / 100). I didn't cast the 100 to double because when one of the arguments to operator/ is a double, the other one is automatically cast (I think).

                                    U Offline
                                    U Offline
                                    User 3150960
                                    wrote on last edited by
                                    #21

                                    double Quantity = 28.88887; double dbRecipeTotalQuantity = 28.88887; int Price = 2425; double dbRecipeTotal = (Quantity / dbRecipeTotalQuantity) * (Price / 100.0);

                                    1 Reply Last reply
                                    0
                                    • R roman_s

                                      How come I get a rounded number when I multiply and assign value to a double??

                                      dbRecipeTotal = (RI.Quantity / dbRecipeTotalQuantity) * (InvItem.Price / 100);

                                      double dbRecipeTotal = 0.0;
                                      RI.Quantity is = 28.88887
                                      dbRecipeTotalQuantity is = 28.88887
                                      InvItem.Price is = 2425

                                      I get dbRecipeTotal = 24.0 in quickview it should be 24.25 Any help is appreaciated

                                      modified on Wednesday, July 28, 2010 9:44 AM

                                      K Offline
                                      K Offline
                                      kxal
                                      wrote on last edited by
                                      #22

                                      I think the InvItem.Price is an integer,if so,the division will diacard the part after decimal point automatically.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups