Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6187,13 +6187,15 @@ def _parse_format_specifier(format_spec, _localeconv=None):
fill = format_dict['fill']
align = format_dict['align']
format_dict['zeropad'] = (format_dict['zeropad'] is not None)

# Support zeropad handling like built-in types (see gh-61449).
if format_dict['zeropad']:
if fill is not None:
raise ValueError("Fill character conflicts with '0'"
" in format specifier: " + format_spec)
if align is not None:
raise ValueError("Alignment conflicts with '0' in "
"format specifier: " + format_spec)
if fill is not None and align is not None:
format_dict['zeropad'] = False
elif align is not None:
format_dict['zeropad'] = False
fill = "0"

format_dict['fill'] = fill or ' '
# PEP 3101 originally specified that the default alignment should
# be left; it was later agreed that right-aligned makes more sense
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@ def test_formatting(self):
('<^+15.20%', 'inf', '<<+Infinity%<<<'),
('\x07>,%', 'sNaN1234567', 'sNaN1234567%'),
('=10.10%', 'NaN123', ' NaN123%'),

# issue 61449
('<06', '1.2', '1.2000'),
('x>06', '1.2', 'xxx1.2'),
]
for fmt, d, result in test_values:
self.assertEqual(format(Decimal(d), fmt), result)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Align support of zero-padding option for :class:`~decimal.Decimal`
formatting with builtin numeric types. Patch by Sergey B Kirpichev.
Loading