python -c "print float(7/3)"
在我的电脑上打印出 2.0。我做错了什么?
这是我正在使用的:
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
最佳答案
7/3
是 Python 2 上的整数除法,因此返回 2
(一个 int
)。然后将其转换为 float
,得到 2.0
。
您可能需要 float(7)/3
、7/float(3)
、7.0/3
或 7/3.0
。
另请注意,您可以执行 from __future__ import division
以便默认情况下除法是 float (然后使用 //
进行整数除法)。
关于python - 为什么python -c "print float(7/3)"打印出2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106299/