python中单位转换_python的单位转换问题

为什么我得出的结果小数点后面只有.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

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值