MCPcopy Index your code
hub / github.com/python/cpython / _format_float_style

Method _format_float_style

Lib/fractions.py:495–597  ·  view source on GitHub ↗

Helper method for __format__; handles float presentation types.

(self, match)

Source from the content-addressed store, hash-verified

493 return sign + padding + body
494
495 def _format_float_style(self, match):
496 """Helper method for __format__; handles float presentation types."""
497 fill = match["fill"] or " "
498 align = match["align"] or ">"
499 pos_sign = "" if match["sign"] == "-" else match["sign"]
500 no_neg_zero = bool(match["no_neg_zero"])
501 alternate_form = bool(match["alt"])
502 zeropad = bool(match["zeropad"])
503 minimumwidth = int(match["minimumwidth"] or "0")
504 thousands_sep = match["thousands_sep"]
505 precision = int(match["precision"] or "6")
506 frac_sep = match["frac_separators"] or ""
507 presentation_type = match["presentation_type"]
508 trim_zeros = presentation_type in "gG" and not alternate_form
509 trim_point = not alternate_form
510 exponent_indicator = "E" if presentation_type in "EFG" else "e"
511
512 if align == '=' and fill == '0':
513 zeropad = True
514
515 # Round to get the digits we need, figure out where to place the point,
516 # and decide whether to use scientific notation. 'point_pos' is the
517 # relative to the _end_ of the digit string: that is, it's the number
518 # of digits that should follow the point.
519 if presentation_type in "fF%":
520 exponent = -precision
521 if presentation_type == "%":
522 exponent -= 2
523 negative, significand = _round_to_exponent(
524 self._numerator, self._denominator, exponent, no_neg_zero)
525 scientific = False
526 point_pos = precision
527 else: # presentation_type in "eEgG"
528 figures = (
529 max(precision, 1)
530 if presentation_type in "gG"
531 else precision + 1
532 )
533 negative, significand, exponent = _round_to_figures(
534 self._numerator, self._denominator, figures)
535 scientific = (
536 presentation_type in "eE"
537 or exponent > 0
538 or exponent + figures <= -4
539 )
540 point_pos = figures - 1 if scientific else -exponent
541
542 # Get the suffix - the part following the digits, if any.
543 if presentation_type == "%":
544 suffix = "%"
545 elif scientific:
546 suffix = f"{exponent_indicator}{exponent + point_pos:+03d}"
547 else:
548 suffix = ""
549
550 # String of output digits, padded sufficiently with zeros on the left
551 # so that we'll have at least one digit before the decimal point.
552 digits = f"{significand:0{point_pos + 1}d}"

Callers 1

__format__Method · 0.95

Calls 5

_round_to_exponentFunction · 0.85
_round_to_figuresFunction · 0.85
rstripMethod · 0.45
joinMethod · 0.45
zfillMethod · 0.45

Tested by

no test coverage detected