I am using python 2.7, and the code I have is:
a = 10.5 * 22.34 / 2.0
print "%.2f" % a
and the result I expect is 117.29, but it shows 117.28. How to solve the problem?
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.
If Python is using a 64-bit IEEE-754 binary floating point type, then the exact value it’s using will be
… and that’s obviously lower than the midpoint between 117.28 and 117.29. It’s possible that that’s what’s going on.
Another option is that Python is using Banker’s Rounding.
If the exact decimal values matter to you, you might want to consider using
decimalinstead.