IceMan said:
I am getting some odd answers when i use doubles in c#
0.55-0.5 = 0.050000000000000044
why am i not getting 0.05
There are some numbers that contain a finite number of decimals when
encoded in Base 10, but which contain infinite decimal digits when converted
into Base 2. The Double datatype stores the binary representation of the
number, and can only contain a limited number of bits (fifty-something bits,
actually). Since the double can only store a finite number of bits, when you
try to store a number whose base-2 representation has infinite bits, the
stream of bits is truncated and only the first bits are kept. If you convert
such a number back into base 10, it will not be exactly the same number that
you tried to store initially. This is why floats and doubles are called
"imprecise" storage (they are not imprecise in base 2, they are just
imprecise representations of base 10 numbers). If you need a perfectly
accurate storage of your decimal numbers, use the type Decimal instead of
Double.