英制转米、磅转千克取小数
Get decimal value when convert inches to meter, pound to kilogram
我想得到这样的结果:
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)
将您的 %d
更改为 %f
:
print "An other way of saying, I am %f meters and %f kilograms." % (meters, kilograms)
如果您希望限制为小数点后两位,那么您可以设置 %.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
限制为小数点后两位:
print "An other way of saying, I am {:.2f} meters and {:.2f} kilograms.".format(meters, kilograms)
我想得到这样的结果: 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)
将您的 %d
更改为 %f
:
print "An other way of saying, I am %f meters and %f kilograms." % (meters, kilograms)
如果您希望限制为小数点后两位,那么您可以设置 %.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
限制为小数点后两位:
print "An other way of saying, I am {:.2f} meters and {:.2f} kilograms.".format(meters, kilograms)