Floating point "g" format not stripping trailing zeros

Hrvoje Nikšić hniksic at gmail.com
Wed Feb 11 15:02:04 EST 2015


According to the documentation of the "g" floating-point format,
trailing zeros should be stripped from the resulting string:

"""
General format. For a given precision p >= 1, this rounds the number
to p significant digits and then formats the result in either
fixed-point format or in scientific notation, depending on its
magnitude.[...]
In both cases insignificant trailing zeros are removed from the
significand, and the decimal point is also removed if there are no
remaining digits following it.
"""

However, in some cases, the trailing zeros apparently remain:

>>> from decimal import Decimal as D
>>> x = D(1)/D(999)
>>> '{:.15g}'.format(x)
'0.00100100100100100'

For floats, the trailing zeros are removed:

>>> '{:.15g}'.format(1. / 999)
'0.001001001001001'

This behavior is present in both 2.7.8 and 3.4.1. Is this a bug in the
formatting of Decimals?



More information about the Python-list mailing list