How can I get the exact result of a multiplication?
Python tells me that 0.017 * 3 equals 0.051000000000000004. Is there any command to make python print 0.051?
Fractional numeric literals in Python have the type float, which is represented in binary. Use the Decimal type for base 10 arithmetic.
>>> from decimal import Decimal
>>> print(Decimal('0.017') * 3)
0.051
Assuming you know the expected number of significant digits the operation should output, you can use the built-in rounding function in python (in this case 3).
>>> round(0.017 * 3, 3)
0.051
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With