2014-03-18 35 views
4

我正在使用Python Decimal類進行精確的浮點運算。我需要將結果編號一致地轉換爲一個標準符號數作爲一個字符串。但是,非常小的十進制數默認以科學記數法呈現。將非常小的python十進制轉換爲非科學記數法字符串

>>> from decimal import Decimal 
>>> 
>>> d = Decimal("0.000001") 
>>> d 
Decimal('0.000001') 
>>> str(d) 
'0.000001' 
>>> d = Decimal("0.000000001") 
>>> d 
Decimal('1E-9') 
>>> str(d) 
'1E-9' 

我怎麼會得到str(d)返回'0.000000001'

回答

5
'{:f}'.format(d) 
Out[12]: '0.000000001' 
+1

有趣的是'.format'使用全精度,'%f'默認精度爲6 – jterrace

+0

好評。我已經檢查過,事實上文檔中指出默認精度爲6,無論是[python2.x](http://docs.python.org/2/library/string.html#formatspec)和[python3]( http://docs.python.org/3/library/string.html#formatspec)。 –

相關問題