Skip to content

Commit f87578f

Browse files
miss-islingtonskirpichevpicnixz
authored
[3.14] gh-130662: Accept leading zeros in precision/width for Decimal's formatting (GH-132549) (#136362)
Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent b2a2f03 commit f87578f

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/_pydecimal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6120,9 +6120,9 @@ def _convert_for_comparison(self, other, equality_op=False):
61206120
(?P<no_neg_0>z)?
61216121
(?P<alt>\#)?
61226122
(?P<zeropad>0)?
6123-
(?P<minimumwidth>(?!0)\d+)?
6123+
(?P<minimumwidth>\d+)?
61246124
(?P<thousands_sep>[,_])?
6125-
(?:\.(?P<precision>0|(?!0)\d+))?
6125+
(?:\.(?P<precision>\d+))?
61266126
(?P<type>[eEfFgGn%])?
61276127
\z
61286128
""", re.VERBOSE|re.DOTALL)

Lib/test/test_decimal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ def test_formatting(self):
982982
('.0f', '0e-2', '0'),
983983
('.0f', '3.14159265', '3'),
984984
('.1f', '3.14159265', '3.1'),
985+
('.01f', '3.14159265', '3.1'), # leading zero in precision
985986
('.4f', '3.14159265', '3.1416'),
986987
('.6f', '3.14159265', '3.141593'),
987988
('.7f', '3.14159265', '3.1415926'), # round-half-even!
@@ -1067,6 +1068,7 @@ def test_formatting(self):
10671068
('8,', '123456', ' 123,456'),
10681069
('08,', '123456', '0,123,456'), # special case: extra 0 needed
10691070
('+08,', '123456', '+123,456'), # but not if there's a sign
1071+
('008,', '123456', '0,123,456'), # leading zero in width
10701072
(' 08,', '123456', ' 123,456'),
10711073
('08,', '-123456', '-123,456'),
10721074
('+09,', '123456', '+0,123,456'),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
+Accept leading zeros in precision and width fields for
2+
+:class:`~decimal.Decimal` formatting, for example ``format(Decimal(1.25),
3+
'.016f')``.

0 commit comments

Comments
 (0)