diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index ef889ea0cc834c..b448a7cf1cbf2d 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -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 diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index fe8c8ce12da0bf..205dd1bbd61e1d 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2026-03-10-19-48-18.gh-issue-61449.0DofsC.rst b/Misc/NEWS.d/next/Library/2026-03-10-19-48-18.gh-issue-61449.0DofsC.rst new file mode 100644 index 00000000000000..7ae56e4a3f9f59 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-10-19-48-18.gh-issue-61449.0DofsC.rst @@ -0,0 +1,2 @@ +Align support of zero-padding option for :class:`~decimal.Decimal` +formatting with builtin numeric types. Patch by Sergey B Kirpichev.