You're doing nothing wrong, and it isn't Python's fault either. Some decimal numbers just cannot be precisely represented as binary floats.
Just like you can't write 1/3
in decimal (0.33333....
), you can't write decimal 0.1
in binary (0.0001100110011001100110011001100110011001100110011...
).
Solution A:
Use print 32.5 * 0.19
- it will automatically round the result.
Solution B:
Use the Decimal module if you actually need this precision, for example when calculating with monetary values.
Solution C:
Use Python 3.2 or Python 2.7 which will automatically round the result in an interactive session.
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 32.50 * 0.19
6.175