【问题标题】:How to prevent/suppress Decimal from using scientific notation?如何防止/抑制 Decimal 使用科学记数法?
【发布时间】:2021-03-22 16:11:48
【问题描述】:
In [1]: from decimal import Decimal

In [2]: Decimal('0.00000100') * Decimal('0.95')
Out[2]: Decimal('9.500E-7')

In [3]: Decimal('9.500E-7').quantize(Decimal('0.00000000'))
Out[3]: Decimal('9.5E-7')

如何压制科学记数法,得到'0.00000095'

【问题讨论】:

  • 请让您的示例可复制粘贴

标签: python decimal scientific-notation


【解决方案1】:

您可以使用Format Specification Mini-Language 格式化输出

from decimal import Decimal
d = Decimal('9.5E-7')
print(d)
print(format(d, 'f'))

输出:

9.5E-7
0.00000095

但是,如果您打算覆盖 decimal.Decimal.__str__decimal.Decimal.__repr__,您会遇到困难,因为它们是只读的。

【讨论】:

    猜你喜欢
    • 2022-10-10
    • 2022-01-21
    • 1970-01-01
    • 2013-07-18
    • 2017-03-13
    • 2020-05-31
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多