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. Other Discussions
  3. Clever Code
  4. Double Trouble

Double Trouble

Scheduled Pinned Locked Moved Clever Code
helpcss
2 Posts 2 Posters 2 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.
  • B Offline
    B Offline
    Ben Fair
    wrote on last edited by
    #1

    I came across this bug today in a report output handler. The application processes property tax data and frequently outputs to delimited text or xls for processing by other apps. It was reported that in a particular output file a dollar amount was one penny less than expected. In the below code, "dr" is a DataRow that is passed to the method and "COL_Check_Amount" is a string constant for the column name in the row. The bug was found when the field in the datarow had the value 4111.86: ((int)(double.Parse(dr[COL_Check_Amount].ToString()) * 100)).ToString().PadLeft(11, '0') If you put this code in a test app and replace dr[COL_Check_Amount] with "4111.86" you find that the resulting INT is 411185. The problem here is that there is a small loss in precision when the DOUBLE is multiplied by 100; the resulting value is 411185.99999999994. Oddly, using FLOAT does not produce the same problem. We resolved the issue by using DECIMAL rather than DOUBLE as is our common practice with monetary values. I'm not sure why DOUBLE could not maintain the precision in what appeared to be a simple calculation.

    D 1 Reply Last reply
    0
    • B Ben Fair

      I came across this bug today in a report output handler. The application processes property tax data and frequently outputs to delimited text or xls for processing by other apps. It was reported that in a particular output file a dollar amount was one penny less than expected. In the below code, "dr" is a DataRow that is passed to the method and "COL_Check_Amount" is a string constant for the column name in the row. The bug was found when the field in the datarow had the value 4111.86: ((int)(double.Parse(dr[COL_Check_Amount].ToString()) * 100)).ToString().PadLeft(11, '0') If you put this code in a test app and replace dr[COL_Check_Amount] with "4111.86" you find that the resulting INT is 411185. The problem here is that there is a small loss in precision when the DOUBLE is multiplied by 100; the resulting value is 411185.99999999994. Oddly, using FLOAT does not produce the same problem. We resolved the issue by using DECIMAL rather than DOUBLE as is our common practice with monetary values. I'm not sure why DOUBLE could not maintain the precision in what appeared to be a simple calculation.

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      Floating point numbers are stored as base 2 not base 10, which means that they have a different set of infinite repeating decimals. .1 is a base2 infinite repeater. If float didn't have a problem you were probably lucky and had it round up and the error truncate away. Decimal is a base 10 representation, and consequently much slower than native floating types. It does however behave exactly like the user naive expectation.

      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