Skip to content

Commit d46ca41

Browse files
committed
gh-130664: treat '0' fill character with align '=' as zero-padding for Fraction's
1 parent 4192ce1 commit d46ca41

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Lib/fractions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ def _format_float_style(self, match):
504504
trim_point = not alternate_form
505505
exponent_indicator = "E" if presentation_type in "EFG" else "e"
506506

507+
if align == '=' and fill == '0':
508+
zeropad = True
509+
507510
# Round to get the digits we need, figure out where to place the point,
508511
# and decide whether to use scientific notation. 'point_pos' is the
509512
# relative to the _end_ of the digit string: that is, it's the number

Lib/test/test_fractions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,12 +1491,7 @@ def test_format_f_presentation_type(self):
14911491
(F('-1234.5678'), '07,.0f', '-01,235'),
14921492
(F('-1234.5678'), '08,.0f', '-001,235'),
14931493
(F('-1234.5678'), '09,.0f', '-0,001,235'),
1494-
# Corner-case - zero-padding specified through fill and align
1495-
# instead of the zero-pad character - in this case, treat '0' as a
1496-
# regular fill character and don't attempt to insert commas into
1497-
# the filled portion. This differs from the int and float
1498-
# behaviour.
1499-
(F('1234.5678'), '0=12,.2f', '00001,234.57'),
1494+
(F('1234.5678'), '0=12,.2f', '0,001,234.57'),
15001495
# Corner case where it's not clear whether the '0' indicates zero
15011496
# padding or gives the minimum width, but there's still an obvious
15021497
# answer to give. We want this to work in case the minimum width
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Handle a corner-case for :class:`~fractions.Fraction`'s formatting: when no
2+
explicit alignment is given, preceding the width field by a zero ('0')
3+
character enables sign-aware zero-padding. Now we treat this as an
4+
equivalent to a fill character of '0' with an alignment type of '=', just as
5+
in case of :class:`float`'s.

0 commit comments

Comments
 (0)