Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc4d3b5

Browse files
committed
Cleanup of __format__ method
1 parent aba35f3 commit fc4d3b5

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

Lib/fractions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,12 @@ def __format__(self, format_spec, /):
435435

436436
# Round to get the digits we need, figure out where to place the point,
437437
# and decide whether to use scientific notation.
438-
n, d = self._numerator, self._denominator
439438
if presentation_type in "fF%":
440-
exponent = -precision - (2 if presentation_type == "%" else 0)
439+
exponent = -precision
440+
if presentation_type == "%":
441+
exponent -= 2
441442
negative, significand = _round_to_exponent(
442-
n, d, exponent, no_neg_zero)
443+
self._numerator, self._denominator, exponent, no_neg_zero)
443444
scientific = False
444445
point_pos = precision
445446
else: # presentation_type in "eEgG"
@@ -448,10 +449,12 @@ def __format__(self, format_spec, /):
448449
if presentation_type in "gG"
449450
else precision + 1
450451
)
451-
negative, significand, exponent = _round_to_figures(n, d, figures)
452+
negative, significand, exponent = _round_to_figures(
453+
self._numerator, self._denominator, figures)
452454
scientific = (
453455
presentation_type in "eE"
454-
or exponent > 0 or exponent + figures <= -4
456+
or exponent > 0
457+
or exponent + figures <= -4
455458
)
456459
point_pos = figures - 1 if scientific else -exponent
457460

@@ -493,8 +496,10 @@ def __format__(self, format_spec, /):
493496
for pos in range(first_pos, len(leading), 3)
494497
)
495498

496-
# Pad with fill character if necessary and return.
499+
# We now have a sign and a body.
497500
body = leading + trailing
501+
502+
# Pad with fill character if necessary and return.
498503
padding = fill * (minimumwidth - len(sign) - len(body))
499504
if align == ">":
500505
return padding + sign + body

0 commit comments

Comments
 (0)