> >>>>> from decimal import Decimal as D > >>>>> x = D(1)/D(999) > >>>>> '{:.15g}'.format(x) > >> > >> '0.00100100100100100' [...] > > I'd say it's a bug. P is 15, you've got 17 digits after the decimal place > > and two of those are insignificant trailing zeros. > > Actually it's the float version that doesn't match the documentation. > In the decimal version, sure there are 17 digits after the decimal > place there, but the first two -- which are leading zeroes -- would > not normally be considered significant.
{:.15g} is supposed to give 15 digits of precision, but with trailing zeros removed. For example, '{:.15g}'.format(Decimal('0.5')) should yield '0.5', not '0.500000000000000' -- and, it indeed does. It is only for some numbers that trailing zeros are not removed, which looks like a bug. The behavior of floats matches both the documentation and other languages using the 'g' decimal format, such as C. > The float version OTOH is only giving you 13 significant digits when > 15 were requested. It is giving 15 significant digits if you count the trailing zeros that have been removed. If those two digits had not been zeros, they would have been included. This is again analogous to '{:.15g}'.format(0.5) returning '0.5'. -- https://mail.python.org/mailman/listinfo/python-list