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培訓機構:千鋒教育。