]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-130664: support '_' (just as ',') in Decimal's formatting (GH-132155) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 14 Jul 2025 12:01:49 +0000 (14:01 +0200)
committerGitHub <noreply@github.com>
Mon, 14 Jul 2025 12:01:49 +0000 (14:01 +0200)
gh-130664: support '_' (just as ',') in Decimal's formatting (GH-132155)
(cherry picked from commit e10fe81cc6ae0979938eb3925139d56a74c620e3)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Lib/_pydecimal.py
Lib/test/test_decimal.py
Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst [new file with mode: 0644]

index ff80180a79e97a138d0ffb1369437dc84d152c15..8d3a9f7ed45e25ea56c52f61994de045e57bf6ff 100644 (file)
@@ -6083,7 +6083,7 @@ _parse_format_specifier_regex = re.compile(r"""\A
 (?P<alt>\#)?
 (?P<zeropad>0)?
 (?P<minimumwidth>(?!0)\d+)?
-(?P<thousands_sep>,)?
+(?P<thousands_sep>[,_])?
 (?:\.(?P<precision>0|(?!0)\d+))?
 (?P<type>[eEfFgGn%])?
 \Z
index 46c50115aef117c5b2f21c78e10479e433509e38..23107c603f8d413151add159d1dcd638e7f32880 100644 (file)
@@ -1059,6 +1059,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 (file)
index 0000000..294a7e0
--- /dev/null
@@ -0,0 +1,2 @@
+Support the ``'_'`` digit separator in formatting of the integral part of
+:class:`~decimal.Decimal`'s.  Patch by Sergey B Kirpichev.