Addition and double value type question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I was calculating some percents and ended up with these two values that I
swa in the debugger (amount1 and amount2 are doubles):
amount1 = 4,999,999.72
amount2 = 0.28000000026077

Then, I added these two together and got 5,000,000.00
Why didn't I get a number like 5000000.00000000026077?

I mean, I got the result I was looking for but how can I be sure what I am
going to get from the addition?

Thanks,
-TK
 
I think this has to do with the precision of the data type.

The decimal data type is more precise. If you perform the same calculation
using decimal, you get the more precise 5000000.0000000026077

-B
 
TK said:
I was calculating some percents and ended up with these two values that I
swa in the debugger (amount1 and amount2 are doubles):
amount1 = 4,999,999.72
amount2 = 0.28000000026077

Then, I added these two together and got 5,000,000.00
Why didn't I get a number like 5000000.00000000026077?

I mean, I got the result I was looking for but how can I be sure what I am
going to get from the addition?

See http://www.pobox.com/~skeet/csharp/floatingpoint.html
 
Back
Top