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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top