Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.6四舍五入到0.0_Python - Fatal编程技术网

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年了。哈哈。谢谢你的帮助!