Skip to content

gh-130664: treat '0' fill character with align '=' as zero-padding for Fraction's #131067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ def _format_float_style(self, match):
trim_point = not alternate_form
exponent_indicator = "E" if presentation_type in "EFG" else "e"

if align == '=' and fill == '0':
zeropad = True

# Round to get the digits we need, figure out where to place the point,
# and decide whether to use scientific notation. 'point_pos' is the
# relative to the _end_ of the digit string: that is, it's the number
Expand Down
7 changes: 2 additions & 5 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,11 +1492,8 @@ def test_format_f_presentation_type(self):
(F('-1234.5678'), '08,.0f', '-001,235'),
(F('-1234.5678'), '09,.0f', '-0,001,235'),
# Corner-case - zero-padding specified through fill and align
# instead of the zero-pad character - in this case, treat '0' as a
# regular fill character and don't attempt to insert commas into
# the filled portion. This differs from the int and float
# behaviour.
(F('1234.5678'), '0=12,.2f', '00001,234.57'),
# instead of the zero-pad character.
(F('1234.5678'), '0=12,.2f', '0,001,234.57'),
# Corner case where it's not clear whether the '0' indicates zero
# padding or gives the minimum width, but there's still an obvious
# answer to give. We want this to work in case the minimum width
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Handle corner-case for :class:`~fractions.Fraction`'s formatting: treat
zero-padding (preceding the width field by a zero (``'0'``) character) as an
equivalent to a fill character of ``'0'`` with an alignment type of ``'='``,
just as in case of :class:`float`'s.
Loading