我的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print (round(1.555, 1)) # It seems normal
print (round(1.555, 2)) # Why it is not output 1.56?
print (round(1.556, 2)) # It seems normal
输出:
sam@sam:~/code/python$ ./t2.py
1.6
1.55
1.56
sam@sam:~/code/python$
round(1.555, 1)
的输出结果为 1.6
。
为什么 round(1.555, 2)
的输出结果不是 1.56
呢?
if x == 2.0
。这是关于浮点运算最重要的事情。 - cxrodgers