Skip to content

Commit c30178a

Browse files
[3.13] gh-130664: Treat '0' fill character with align '=' as zero-padding for Fraction's (GH-131067) (GH-136242)
(cherry picked from commit c113a8e) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent fc77192 commit c30178a

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Lib/fractions.py

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

484+
if align == '=' and fill == '0':
485+
zeropad = True
486+
484487
# Round to get the digits we need, figure out where to place the point,
485488
# and decide whether to use scientific notation. 'point_pos' is the
486489
# relative to the _end_ of the digit string: that is, it's the number

Lib/test/test_fractions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,11 +1411,8 @@ def test_format_f_presentation_type(self):
14111411
(F('-1234.5678'), '08,.0f', '-001,235'),
14121412
(F('-1234.5678'), '09,.0f', '-0,001,235'),
14131413
# Corner-case - zero-padding specified through fill and align
1414-
# instead of the zero-pad character - in this case, treat '0' as a
1415-
# regular fill character and don't attempt to insert commas into
1416-
# the filled portion. This differs from the int and float
1417-
# behaviour.
1418-
(F('1234.5678'), '0=12,.2f', '00001,234.57'),
1414+
# instead of the zero-pad character.
1415+
(F('1234.5678'), '0=12,.2f', '0,001,234.57'),
14191416
# Corner case where it's not clear whether the '0' indicates zero
14201417
# padding or gives the minimum width, but there's still an obvious
14211418
# answer to give. We want this to work in case the minimum width
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Handle corner-case for :class:`~fractions.Fraction`'s formatting: treat
2+
zero-padding (preceding the width field by a zero (``'0'``) character) as an
3+
equivalent to a fill character of ``'0'`` with an alignment type of ``'='``,
4+
just as in case of :class:`float`'s.

0 commit comments

Comments
 (0)