[issue30453] str.format() method, Rounding off wrong

Kazuhiro Fujie report at bugs.python.org
Thu May 25 05:08:27 EDT 2017


Kazuhiro Fujie added the comment:

Hi, Mark

Thank you for teaching politely.

I had misunderstood about format() and round() functions.
I made mistakes though I already knew about floating point.
I think try to take this tactics on my codes.

num1 = 4.625
num2 = 4.62501
num3 = 4.62500000000001
num4 = 4.625000000000001
num5 = 4.6250000000000001
print(num1, num2, num3, num4, num5) 
#=> 4.625 4.62501 4.62500000000001 4.625000000000001 4.625

print("{0:.2f} : {1:.2f} : {2:.2f} : {3:.2f} : {4:.2f}".format(num1, num2, num3, num4, num5))
#=> 4.62 : 4.63 : 4.63 : 4.63 : 4.62

print("{0:.3f} : {1:.3f} : {2:.3f} : {3:.3f} : {4:.3f}".format(num1, num2, num3, num4, num5))
#=> 4.625 : 4.625 : 4.625 : 4.625 : 4.625

print("{0:.4f} : {1:.4f} : {2:.4f} : {3:.4f} : {4:.4f}".format(num1, num2, num3, num4, num5))
#=> 4.6250 : 4.6250 : 4.6250 : 4.6250 : 4.6250

print("{0:.16f} : {1:.16f} : {2:.16f} : {3:.16f} : {4:.16f}".format(num1, num2, num3, num4, num5))
#=> 4.6250000000000000 : 4.6250099999999996 : 4.6250000000000098 : 4.6250000000000009 : 4.6250000000000000
data = 4.625
plus = 0.000000000000001
num = data + plus
print("{0:.2f}".format(num))    #=> 4.63


Thank you very much.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30453>
_______________________________________


More information about the Python-bugs-list mailing list