Discuss / Python / python的round简直恐怖~

python的round简直恐怖~

Topic source

-- coding: utf-8 --

height=float(input('请输入你的身高:')) weight=float(input('请输入你的体重:')) bmi=round(weight/(height*height),2) if bmi<18.5: print('bmi指数为:%.2f 过轻'%bmi) elif 18.5<=bmi<=25: print('bmi指数为:%.2f 正常'%bmi) elif 28<=bmi<=32: print('bmi指数为:%.2f 肥胖'%bmi) else: print('bmi指数为:%.2f 严重肥胖'%bmi)

本想用round()对bmi的float尾数进行四舍五入处理,结果问题恐怖

round(1.675, 2) 1.68 round(2.675, 2) 2.67

网上也查了不少,小小的问题涉及python在底层存储数据的精度控制问题,还是个菜鸟,不糙那个心了

bmi=round(weight/(height*height),2)

这行作废,取精度还是在输出时解决 " %.2f "


  • 1

Reply