python里小数如何表述?

2023-12-13 11:30:49 网络知识 匿名

1、Python中的小数存在精度问题:

>>>0.1+0.1+0.1-0.3

5.551115123125783e-17

>>>print(0.1+0.1+0.1-0.3)

5.551115123125783e-17

2、那小数怎么表示呢?

>>>fromdecimalimportDecimal

>>>Decimal("0.1")+Decimal("0.1")+Decimal("0.1")-Decimal("0.3")

Decimal('0.0')

Decimal可以很好的解决小数的运算问题,这对于银行这种需要精确计算的是很方便的。

Decimal还可以输入的小数位数自动升级位数。

>>>Decimal("0.1")+Decimal("0.10")+Decimal("0.10")-Decimal("0.30")

Decimal('0.00')

以上内容为大家介绍了python里小数如何表述?希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:瀚银百科。

发表评论: