2 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
試試這個(gè)decimal模塊:
from decimal import Decimal, getcontext
# set desired precision, 30 for example
getcontext().prec = 30
# normal
print(1 / 7)
# 0.14285714285714285
# with Decimal
print(Decimal(1) / Decimal(7))
# 0.142857142857142857142857142857

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
評(píng)論是正確的,format()是不準(zhǔn)確的。
您可以使用十進(jìn)制模塊。
from decimal import *
getcontext().prec = 6 #set the number of decimals you prefer
Decimal(1) / Decimal(7)
>>> Decimal('0.142857')
getcontext().prec = 28
Decimal(1) / Decimal(7)
>>> Decimal('0.1428571428571428571428571429')
添加回答
舉報(bào)