use*_*288 0 python floating-point
如何在python中进行浮点计算?
for j in range(10 , 50):
print "(10/(j+1)) is %f " % (10/(j+1))
Run Code Online (Sandbox Code Playgroud)
为什么输出都是0?
j必须是整数,但我需要浮动结果.
谢谢
在Python2中,/将两个整数的商截断为整数,
你可以做到
for j in range(10 , 50):
print "(10/(j+1)) is %f " % (10.0/(j+1))
Run Code Online (Sandbox Code Playgroud)
或者不是全局截断,就像Python3一样
from __future__ import division
for j in range(10 , 50):
print "(10/(j+1)) is %f " % (10/(j+1))
Run Code Online (Sandbox Code Playgroud)