From 75fde5d061dbf6f82cc10176de097a4ccc3e763c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 15 Apr 2025 14:38:30 +0300 Subject: [PATCH 1/3] gh-130662: accept leading zeros in precision/width for Decimal's formatting --- Lib/_pydecimal.py | 4 ++-- Lib/test/test_decimal.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 38dc7b70e0f6f0..6534f644cf567a 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -6097,9 +6097,9 @@ def _convert_for_comparison(self, other, equality_op=False): (?Pz)? (?P\#)? (?P0)? -(?P(?!0)\d+)? +(?P\d+)? (?P[,_])? -(?:\.(?P0|(?!0)\d+))? +(?:\.(?P\d+))? (?P[eEfFgGn%])? \Z """, re.VERBOSE|re.DOTALL) diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index fb14b80f7a8a2b..c90c10c0bc8942 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -982,6 +982,7 @@ def test_formatting(self): ('.0f', '0e-2', '0'), ('.0f', '3.14159265', '3'), ('.1f', '3.14159265', '3.1'), + ('.01f', '3.14159265', '3.1'), # leading zero in precision ('.4f', '3.14159265', '3.1416'), ('.6f', '3.14159265', '3.141593'), ('.7f', '3.14159265', '3.1415926'), # round-half-even! @@ -1067,6 +1068,7 @@ def test_formatting(self): ('8,', '123456', ' 123,456'), ('08,', '123456', '0,123,456'), # special case: extra 0 needed ('+08,', '123456', '+123,456'), # but not if there's a sign + ('008,', '123456', '0,123,456'), # leading zero in width (' 08,', '123456', ' 123,456'), ('08,', '-123456', '-123,456'), ('+09,', '123456', '+0,123,456'), From 251ce802d5a38947437f4d71242c301b35e7bf5d Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Jun 2025 14:37:12 +0300 Subject: [PATCH 2/3] address review: add news --- .../Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst new file mode 100644 index 00000000000000..da91107c7f31a3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst @@ -0,0 +1,3 @@ ++Accept leading zeros in precision/width fields for ++:class:`~decimal.Decimal` formatting, for example ``format(Decimal(1.25), +'.016f')``. From eb4d8b4ba09d4c65ed97d4e70f321c15cecfef87 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Jun 2025 14:55:55 +0300 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst index da91107c7f31a3..d97d937376a9d0 100644 --- a/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst +++ b/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst @@ -1,3 +1,3 @@ -+Accept leading zeros in precision/width fields for ++Accept leading zeros in precision and width fields for +:class:`~decimal.Decimal` formatting, for example ``format(Decimal(1.25), '.016f')``.