Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85547 > unrolled thread

Floating point "g" format not stripping trailing zeros

Started byHrvoje Nikšić <hniksic@gmail.com>
First post2015-02-11 21:02 +0100
Last post2015-02-11 21:02 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Floating point "g" format not stripping trailing zeros Hrvoje Nikšić <hniksic@gmail.com> - 2015-02-11 21:02 +0100

#85547 — Floating point "g" format not stripping trailing zeros

FromHrvoje Nikšić <hniksic@gmail.com>
Date2015-02-11 21:02 +0100
SubjectFloating point "g" format not stripping trailing zeros
Message-ID<mailman.18665.1423684925.18130.python-list@python.org>
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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web