我有两个整数值a
and b
,但我需要它们的浮点比率。我知道a < b
我想计算a / b
,所以如果我使用整数除法,我总是会得到 0,余数为a
.
我怎样才能强迫c
是Python 2中的浮点数吗?
c = a / b
In 3.x, the behaviour is reversed; see Why does integer division yield a float instead of another integer? https://stackoverflow.com/questions/1282945 for the opposite, 3.x-specific problem.
在 Python 2 中,两个 int 相除会生成一个 int。在 Python 3 中,它生成一个浮点数。我们可以通过导入来获得新的行为__future__
.
>>> from __future__ import division
>>> a = 4
>>> b = 6
>>> c = a / b
>>> c
0.66666666666666663
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)