Possible Duplicate:
python limiting floats to two decimal points
If I make this: 12.45-12
in Python the answer I get is: 0.44999999999999929
I do I do it to make it: 0.45?
BTW I did remember to do: 12.45-12.0
But no result.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a result of the fact that many decimals cannot be exactly represented in binary.
For example, 0.25 can: it’s
0.01(0*1, 0*1/2, 1*1/4). 0.1 can’t (0.0001100110011...), just like you can’t write 1/3 as a complete decimal (0.3333333333...).If you do
you get
because
printonly displays the first significant digits.See the Python docs for an excellent summary.
If you do care about decimal values being exact (for example to avoid a Superman III scenario in a financial institution), look at the Decimal module.