Skip to content

Commit 9c72658

Browse files
skirpichevpicnixz
andauthored
gh-130662: Accept leading zeros in precision/width for Decimal's formatting (#132549)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 5bc2d99 commit 9c72658

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
@@ -981,6 +981,7 @@ def test_formatting(self):
981981
('.0f', '0e-2', '0'),
982982
('.0f', '3.14159265', '3'),
983983
('.1f', '3.14159265', '3.1'),
984+
('.01f', '3.14159265', '3.1'), # leading zero in precision
984985
('.4f', '3.14159265', '3.1416'),
985986
('.6f', '3.14159265', '3.141593'),
986987
('.7f', '3.14159265', '3.1415926'), # round-half-even!
@@ -1066,6 +1067,7 @@ def test_formatting(self):
10661067
('8,', '123456', ' 123,456'),
10671068
('08,', '123456', '0,123,456'), # special case: extra 0 needed
10681069
('+08,', '123456', '+123,456'), # but not if there's a sign
1070+
('008,', '123456', '0,123,456'), # leading zero in width
10691071
(' 08,', '123456', ' 123,456'),
10701072
('08,', '-123456', '-123,456'),
10711073
('+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)