将英寸转换为米,磅转换为千克时获取十进制值

时间:2015-11-16 18:07:53

标签: python decimal

我想得到这样的结果:     100磅= 45.3592     50英寸= 1.27米 我该怎么做? 这是我的代码:

name = 'X'
age = 25 
height = 50 
weight = 100 
eyes = 'Black'
teeth = 'White'
hair = 'Black'
meters = 0.0254 * float(50)
kilograms = 0.453592 * float(100)

print "Let's talk about %s." % name
print "She's %d inches tall." % height
print "She's %d pounds heavy." % weight
print "Actually that's too skinny."
print "She's got %s eyes and %s hair." % (eyes, hair)
print "Her teeth are usually %s depending on the coffee." % teeth

print "If I add %d, %d and %d I get %d." % (age, height, weight, age + height + weight)
print "An other way of saying, I am %d meters and %d kilograms." % (meters, kilograms)

1 个答案:

答案 0 :(得分:3)

将您的%d更改为%f

print "An other way of saying, I am %f meters and %f kilograms." % (meters, kilograms)

如果您希望限制为2位小数,则可以设置%.2f

print "An other way of saying, I am %.2f meters and %.2f kilograms." % (meters, kilograms)

或者,您可以使用format

print "An other way of saying, I am {} meters and {} kilograms.".format(meters, kilograms)

使用format限制为2位小数:

print "An other way of saying, I am {:.2f} meters and {:.2f} kilograms.".format(meters, kilograms)

查看string formatting operators