python精确计算浮点数
因为二进制计算的问题,部分浮点数不能精确计算,如
>>>1.1 + 2.2
3.30000000000003
可以使用 round(number,保留位数)
或decimal包
from decimal import Decimal
a = Decimal('1.1') + Decimal('2.2')
float(a)#a=3.3
因为二进制计算的问题,部分浮点数不能精确计算,如
>>>1.1 + 2.2
3.30000000000003
可以使用 round(number,保留位数)
或decimal包
from decimal import Decimal
a = Decimal('1.1') + Decimal('2.2')
float(a)#a=3.3