0

I am getting a value of a variable from a Gridview (Devexpress XtraGrid). When i multiply the value with 100, it loses its precision. Here is the code i'm using

//Value written on the cell is 0.285
double Width = double.Parse(grdvwDimension.GetRowCellValue(0, "Width");
//Now the value of Width = 0.285

Width = Width * 100;
//Now the value of Width = 28.499999999999996
//It should have been 28.5

I've run into this kind of problem in another project but i found out that i should use either float or double consistently thorough out the project, not both.

I tried some other values, sometimes it works correctly (0.385 becomes 38.5), and sometimes it doesn't (0.274 becomes 27.400000000000002).

I checked what the datatype of the column is, its double. I'm using double consistently in my code.

So, what could be the problem?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3659497
  • 445
  • 1
  • 6
  • 15
  • 2
    Use `decimal` and you're done. http://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net Maybe this is even a duplicate: http://stackoverflow.com/questions/1420752/is-double-multiplication-broken-in-net – Tim Schmelter Apr 29 '15 at 11:04
  • http://en.wikipedia.org/wiki/Floating_point – idstam Apr 29 '15 at 11:08
  • What about when saving into the database. Calculate everything in decimal, then convert to double and save? – user3659497 Apr 29 '15 at 11:12

0 Answers0