为什么我得出的结果小数点后面只有.00而不是其他精确数字呢。python把结果四舍五入到整数。请问我哪里出错了呢?print"welcometoourweightconversionprogram!"#promptfornumberandcon...
为什么我得出的结果小数点后面只有 .00 而不是其他精确数字呢。python把结果四舍五入到整数。请问我哪里出错了呢?
print "welcome to our weight conversion program!"
# prompt for number and confirm it
x=raw_input('give a weight:')
fx=float(x)
print '->your weight is %.2f' % fx
# compute the weight formatting
lb = round(fx*2.205) # one pound is 0.4536 kilograms
gal = round(fx*0.2642) # one gallon is 3.785 kilograms
l = fx # one liter is equal one kilogram
# build the first output line with result
S = '%.2f kg=' % float(x)
R = '%.2f lb=' % lb
R += '%.2f gal=' % gal
R += '%.2f l' % l
print S+R #print first output line
# check and build second output line
kg = round(fx*0.4436) # one kiogram is 2.205 pounds
gal = round(fx*0.1198) #one gallon is 8.345 pounds
l = round(fx*0.4534) # one liter is 2.205 pounds
# build the second output line with result
P = '%.2f lb=' % float(x)
Q = '%.2f kg=' % kg
Q += '%.2f gal=' % gal
Q += '%.2f l' % l
print P+Q #print second ourput line
我得出的结果是
python weights.py
welcome to our weight conversion program!
give a weight:123.456
->your weight is 123.46
123.46 kg=272.00 lb=33.00 gal=123.46 l
123.46 lb=55.00 kg=15.00 gal=56.00 l
展开