Skip to content

Commit caefd01

Browse files
committed
pythongh-130662: make Fraction's formatting more compatible wrt float's
1 parent 9211b3d commit caefd01

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

Lib/fractions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ def _round_to_figures(n, d, figures):
168168
# A '0' that's *not* followed by another digit is parsed as a minimum width
169169
# rather than a zeropad flag.
170170
(?P<zeropad>0(?=[0-9]))?
171-
(?P<minimumwidth>0|[1-9][0-9]*)?
171+
(?P<minimumwidth>[0-9]+)?
172172
(?P<thousands_sep>[,_])?
173-
(?:\.(?P<precision>0|[1-9][0-9]*))?
173+
(?:\.(?P<precision>[0-9]+))?
174174
(?P<presentation_type>[eEfFgG%])
175175
""", re.DOTALL | re.VERBOSE).fullmatch
176176

Lib/test/test_fractions.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,8 @@ def test_format_f_presentation_type(self):
15301530
(F(51, 1000), '.1f', '0.1'),
15311531
(F(149, 1000), '.1f', '0.1'),
15321532
(F(151, 1000), '.1f', '0.2'),
1533+
(F(1, 3), '.016f', '0.3333333333333333'), # issue gh-130662
1534+
(F(1, 3), '0030.016f', '0000000000000.3333333333333333'),
15331535
]
15341536
for fraction, spec, expected in testcases:
15351537
with self.subTest(fraction=fraction, spec=spec):
@@ -1628,12 +1630,6 @@ def test_invalid_formats(self):
16281630
'=010%',
16291631
'>00.2f',
16301632
'>00f',
1631-
# Too many zeros - minimum width should not have leading zeros
1632-
'006f',
1633-
# Leading zeros in precision
1634-
'.010f',
1635-
'.02f',
1636-
'.000f',
16371633
# Missing precision
16381634
'.e',
16391635
'.f',

0 commit comments

Comments
 (0)