为什么没有浮动输出?

时间:2015-08-01 22:13:33

标签: python powershell floating-point-conversion

刚开始学习Python。为什么当我在PowerShell中运行此程序时,结果是第9行和第10行没有浮动数字?

height = 74.0 #inches
weight = 180.0 #lbs

#converts from inches to cm/ and from lbs to kg.
cm = height * 2.5400    
kg = weight * 0.453592

print ""
print "Height %d inches in cm is %d " % (height, cm)    
print "Weight in %d lbs in kg is %d " % (weight, kg)

print type(kg)
print type(cm)

1 个答案:

答案 0 :(得分:2)

您使用了%d,用于打印整数。它将浮点数转换为整数。

我建议使用format方法而不是%

print 'Height {} inches in cm is {}'.format(height, cm)

尽管使用%

做到这一点非常简单
print "Height %s inches in cm is %s" % (height, cm)

%s在参数上调用str