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 28 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.
  • 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