From 1ae18f5963365525cf1506bb4efb616ccff19869 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 6 Apr 2025 14:34:46 +0300 Subject: [PATCH 1/2] gh-130664: support '_' (just as ',') in Decimal's formatting ```pycon >>> from _decimal import Decimal as D >>> format(D(1234567), '_') '1_234_567' >>> format(D(1234567), '020_') '0_000_000_001_234_567' >>> format(D('1234.56'), '07_') '1_234.56' >>> format(D('1.23456789'), '_') '1.23456789' >>> format(D('123.456789'), '_%') '12_345.6789%' >>> from _pydecimal import Decimal as D >>> format(D(1234567), '_') '1_234_567' >>> format(D(1234567), '020_') '0_000_000_001_234_567' >>> format(D('1234.56'), '07_') '1_234.56' >>> format(D('1.23456789'), '_') '1.23456789' >>> format(D('123.456789'), '_%') '12_345.6789%' ``` --- Lib/_pydecimal.py | 2 +- Lib/test/test_decimal.py | 5 +++++ .../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 ec036199331396..38dc7b70e0f6f0 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -6098,7 +6098,7 @@ def _convert_for_comparison(self, other, equality_op=False): (?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 d2327d247fa498..b97f3ddd4d0ce9 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1082,6 +1082,11 @@ def test_formatting(self): (',%', '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 00000000000000..de199f0f529d78 --- /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 ``'_'`` digit separator in formatting of the integral part of +:class:`~decimal.Decimal`'s. Patch by Sergey B Kirpichev. From e0dbe8900303566edb6d177e875049044e1a5a15 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 7 Apr 2025 04:57:21 +0300 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- .../next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index de199f0f529d78..294a7e031b2806 100644 --- 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 @@ -1,2 +1,2 @@ -Support ``'_'`` digit separator in formatting of the integral part of +Support the ``'_'`` digit separator in formatting of the integral part of :class:`~decimal.Decimal`'s. Patch by Sergey B Kirpichev.