[New-bugs-announce] [issue5864] format(1234.5, '.4') gives misleading result

Mark Dickinson report at bugs.python.org
Tue Apr 28 13:38:21 CEST 2009


New submission from Mark Dickinson <dickinsm at gmail.com>:

In all versions of Python from 2.6 up, I get the following behaviour:

>>> format(123.456, '.4')
'123.5'
>>> format(1234.56, '.4')
'1235.0'
>>> format(12345.6, '.4')
'1.235e+04'

The first and third results are as I expect, but the second is somewhat
misleading: it gives 5 significant digits when only 4 were requested,
and moreover the last digit is incorrect.

I propose that Python 2.7 and Python 3.1 be changed so that the output
for the second line above is '1.235e+03'.

Note that in both Python and C, '%.<precision>g' formatting switches to
exponential notation at 1e<precision> precisely to avoid the problem of
producing more significant digits than were requested;  I'm proposing
that the same solution be applied for '' formatting, except that since
the empty format code is always required to produce at least one digit
after the decimal point, the switch should happen at 1e<precision-1>
instead of 1e<precision>.

This change should not be backported to 2.6 or 3.0 since there's a small
risk of breakage (particular in doctests).

----------
components: Interpreter Core
messages: 86728
nosy: eric.smith, marketdickinson
severity: normal
stage: needs patch
status: open
title: format(1234.5, '.4') gives misleading result
type: behavior
versions: Python 2.7, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5864>
_______________________________________


More information about the New-bugs-announce mailing list