Str*_*ala 1 python floating-point int
观察以下python程序
def goo(y,x):
y = float(y)
x = float(x)
yup = (y - x - 1) / x
yup = str(yup)
yup = yup.split(".")
decimal = "." + yup[1]
decimal = float(decimal)
output = decimal / (1/x)
print output
print int(output)
goo(9,5)
Run Code Online (Sandbox Code Playgroud)
当程序执行"打印输出"时,答案是3.0
当程序执行"print int(output)"时,答案是2
"print int(output)"应该执行以提供3而不是2
为什么会这样?
该print语句的默认值是将值四舍五入到最近.int()截断.
>>> print 2.99999999999999
3.0
>>> print int(2.99999999999999)
2
Run Code Online (Sandbox Code Playgroud)
如果要查看更精确的表示,请使用repr.
>>> print repr(2.9999999999999)
2.9999999999999
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144 次 |
| 最近记录: |