Python四舍五入变量打印0
我有一个程序中的下一个片段:Python四舍五入变量打印0,python,python-3.x,Python,Python 3.x,我有一个程序中的下一个片段: #weight in kg weight = float(input('Enter your weight: ')) #height in cm height = float(input('Enter your height: ')) bmi_metric = weight/(height**2)/(100**2) print("{:.2f}".format(bmi_metric)) 问题是当我尝试使用round()函数进行舍入时,它在控制台中吐出0。如果我让未
#weight in kg
weight = float(input('Enter your weight: '))
#height in cm
height = float(input('Enter your height: '))
bmi_metric = weight/(height**2)/(100**2)
print("{:.2f}".format(bmi_metric))
问题是当我尝试使用round()函数进行舍入时,它在控制台中吐出0。如果我让未格式化,它会给我一个正确的数字,有很多小数。
这是控制台:
Enter your weight: 44
Enter your height: 182
0.000
一切正常(在您的情况下,bmi_指标非常小,所以四舍五入确实为0),但您的bmi计算公式错误:
#weight in kg
weight = float(input('Enter your weight: '))
#height in cm
height = float(input('Enter your height: '))
bmi_metric = weight/((height/100)**2)
print("{:.2f}".format(bmi_metric))
但你正在把输出四舍五入。如果你得到的值小于你的精度水平,你希望得到什么样的输出?我相信事实上,浮点的行为很奇怪。不,这不是因为“浮点的行为很奇怪”,而是因为你得到的数字小于
0.000
。实际上是0.000000133
。你得到的输出是预期的。然后问题是你的数学,而不是程序输出的是什么。bmi_metric=weight/((身高^2)/(100^2))@roganjosh你是对的,忘记了分数括号