Skip to content

Commit e252a34

Browse files
committed
gh-61449: handle 0-padding option for Decimals like for floats
1 parent 66eafc9 commit e252a34

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Lib/_pydecimal.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6187,13 +6187,15 @@ def _parse_format_specifier(format_spec, _localeconv=None):
61876187
fill = format_dict['fill']
61886188
align = format_dict['align']
61896189
format_dict['zeropad'] = (format_dict['zeropad'] is not None)
6190+
6191+
# Support zeropad handling like built-in types (see gh-61449).
61906192
if format_dict['zeropad']:
6191-
if fill is not None:
6192-
raise ValueError("Fill character conflicts with '0'"
6193-
" in format specifier: " + format_spec)
6194-
if align is not None:
6195-
raise ValueError("Alignment conflicts with '0' in "
6196-
"format specifier: " + format_spec)
6193+
if fill is not None and align is not None:
6194+
format_dict['zeropad'] = False
6195+
elif align is not None:
6196+
format_dict['zeropad'] = False
6197+
fill = "0"
6198+
61976199
format_dict['fill'] = fill or ' '
61986200
# PEP 3101 originally specified that the default alignment should
61996201
# be left; it was later agreed that right-aligned makes more sense

Lib/test/test_decimal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,10 @@ def test_formatting(self):
11641164
('<^+15.20%', 'inf', '<<+Infinity%<<<'),
11651165
('\x07>,%', 'sNaN1234567', 'sNaN1234567%'),
11661166
('=10.10%', 'NaN123', ' NaN123%'),
1167+
1168+
# issue 61449
1169+
('<06', '1.2', '1.2000'),
1170+
('x>06', '1.2', 'xxx1.2'),
11671171
]
11681172
for fmt, d, result in test_values:
11691173
self.assertEqual(format(Decimal(d), fmt), result)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Align support of zero-padding option for :class:`~decimal.Decimal`
2+
formatting with builtin numeric types. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)