Python 3.6四舍五入到0.0
我遇到问题。我有以下代码:Python 3.6四舍五入到0.0,python,Python,我遇到问题。我有以下代码: import json import math class Person: def __init__(self, first_name="", last_name="", age=0, height=0, weight=0): self.first_name = first_name self.last_name = last_name self.age = age self.height =
import json
import math
class Person:
def __init__(self, first_name="", last_name="", age=0, height=0, weight=0):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.height = height
self.weight = weight
self.bmi = self.__bmi()
def __bmi(self):
a = (self.weight / math.exp(self.height)) * 703
print(a)
b = round(a, 1)
return b
def print_json(self):
print(json.dumps(self.__dict__))
Jane = Person(first_name="Jane", last_name="Doe", age=35, height=72, weight=130)
Jane.print_json()
通过执行上述代码,我得到了输出:
4.916952131643318e-27
{"height": 72, "weight": 130, "age": 35, "bmi": 0, "last_name": "Doe", "first_name": "Jane"}
它将
4.916952131643318e-27
四舍五入到0.0
。我正试着四舍五入到4.9
你所看到的数字实际上是科学记数法。注意“e-27”——这意味着它实际上是4.9*10^-27,或者4.9除以1,后面是27 0。这显然非常非常接近于零。将此作为一条评论,我想我不妨将其转化为一个答案
4.9e-27
是一个非常小的数字(0.00000000000000000000000000000000000049),因此四舍五入到1位数为0。而且,你的体重指数计算结果看起来还是有点不对劲math.exp(x)
将e
赋予x
幂。我相信你想要(self.weight/(self.height**2))*703
你为什么要将0.000000000000000000000000004916952131643318四舍五入到4.9?4.9e-27是一个非常小的数字,四舍五入到1位是0。另外,你的BMI计算结果看起来也不太对劲。math.exp将e
赋予x
幂。我相信你想要(self.weight/(self.height**2))*703
。谢谢马克和@17slim的回复。我的意思是轮(4.916952131643318e-27*1e27,1)
会满足OP的要求,我想……谢谢你的解释。请参阅我对已接受答案的评论。:)我知道我一定是做错了什么。这很有效。我已经离开学校17年了。哈哈。谢谢你的帮助!