From e10fe81cc6ae0979938eb3925139d56a74c620e3 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 15 Apr 2025 13:38:03 +0300 Subject: gh-130664: support '_' (just as ',') in Decimal's formatting (#132155) --- Lib/_pydecimal.py | 2 +- Lib/test/test_decimal.py | 5 +++++ .../next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index ec03619..38dc7b7 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -6098,7 +6098,7 @@ _parse_format_specifier_regex = re.compile(r"""\A (?P\#)? (?P0)? (?P(?!0)\d+)? -(?P,)? +(?P[,_])? (?:\.(?P0|(?!0)\d+))? (?P[eEfFgGn%])? \Z diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 323a4d2..fb14b80 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1083,6 +1083,11 @@ class FormatTest: (',%', '123.456789', '12,345.6789%'), (',e', '123456', '1.23456e+5'), (',E', '123456', '1.23456E+5'), + # ... with '_' instead + ('_', '1234567', '1_234_567'), + ('07_', '1234.56', '1_234.56'), + ('_', '1.23456789', '1.23456789'), + ('_%', '123.456789', '12_345.6789%'), # negative zero: default behavior ('.1f', '-0', '-0.0'), diff --git a/Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst b/Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst new file mode 100644 index 0000000..294a7e0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst @@ -0,0 +1,2 @@ +Support the ``'_'`` digit separator in formatting of the integral part of +:class:`~decimal.Decimal`'s. Patch by Sergey B Kirpichev. -- cgit v0.12