[issue43315] Decimal.__str__ has no way to force exact decimal representation

Mark Dickinson report at bugs.python.org
Wed Feb 24 07:03:57 EST 2021


Mark Dickinson <dickinsm at gmail.com> added the comment:

This is by design: the choice of -6 is not arbitrary, but follows the standard that the Decimal class is based on. Quoting from http://speleotrove.com/decimal/daconvs.html#reftostr:

> If the exponent is less than or equal to zero and the adjusted exponent is greater than or equal to -6, [...]

"str" isn't parameterizable (at least, not without a major change to the way that Python works), so if you're using it you have to accept the compromises it makes. If you want finer control over the string representation of Decimal objects, use Python's formatting capabilities:

    >>> from decimal import Decimal
    >>> x = Decimal("1e-8")
    >>> format(x, 'f')
    '0.00000001'

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43315>
_______________________________________


More information about the Python-bugs-list mailing list