[New-bugs-announce] [issue35546] String formatting produces incorrect result with left-aligned zero-padded format

Serhiy Storchaka report at bugs.python.org
Thu Dec 20 09:41:00 EST 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

Compare printf-style string formatting and new-style string formatting.

>>> '%-020d' % 42
'42                  '
>>> format(42, '<020')
'42000000000000000000'
>>> format(42, '<020d')
'42000000000000000000'
>>> '%-020x' % 42
'2a                  '
>>> format(42, '<020x')
'2a000000000000000000'
>>> '%-020g' % 1.2e-8
'1.2e-08             '
>>> format(1.2e-8, '<020')
'1.2e-080000000000000'
>>> format(1.2e-8, '<020g')
'1.2e-080000000000000'
>>> format(1.2e-8, '<020e')
'1.200000e-0800000000'

New-style string formatting produces the result that looks like a correctly formatted number, but it represents incorrect number.

I think that zero padding should not be allowed for left-aligned format for numbers (except the 'f' format). Zero padding is already disallowed for complex numbers.

----------
components: Interpreter Core
messages: 332231
nosy: eric.smith, serhiy.storchaka
priority: normal
severity: normal
status: open
title: String formatting produces incorrect result with left-aligned zero-padded format

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


More information about the New-bugs-announce mailing list