MCPcopy Index your code
hub / github.com/RustPython/RustPython / __format__

Method __format__

Lib/_pydecimal.py:3722–3806  ·  view source on GitHub ↗

Format a Decimal instance according to the given specifier. The specifier should be a standard format specifier, with the form described in PEP 3101. Formatting types 'e', 'E', 'f', 'F', 'g', 'G', 'n' and '%' are supported. If the formatting type is omitted it defa

(self, specifier, context=None, _localeconv=None)

Source from the content-addressed store, hash-verified

3720 # PEP 3101 support. the _localeconv keyword argument should be
3721 # considered private: it's provided for ease of testing only.
3722 def __format__(self, specifier, context=None, _localeconv=None):
3723 """Format a Decimal instance according to the given specifier.
3724
3725 The specifier should be a standard format specifier, with the
3726 form described in PEP 3101. Formatting types 'e', 'E', 'f',
3727 'F', 'g', 'G', 'n' and '%' are supported. If the formatting
3728 type is omitted it defaults to 'g' or 'G', depending on the
3729 value of context.capitals.
3730 """
3731
3732 # Note: PEP 3101 says that if the type is not present then
3733 # there should be at least one digit after the decimal point.
3734 # We take the liberty of ignoring this requirement for
3735 # Decimal---it's presumably there to make sure that
3736 # format(float, '') behaves similarly to str(float).
3737 if context is None:
3738 context = getcontext()
3739
3740 spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
3741
3742 # special values don't care about the type or precision
3743 if self._is_special:
3744 sign = _format_sign(self._sign, spec)
3745 body = str(self.copy_abs())
3746 if spec['type'] == '%':
3747 body += '%'
3748 return _format_align(sign, body, spec)
3749
3750 # a type of None defaults to 'g' or 'G', depending on context
3751 if spec['type'] is None:
3752 spec['type'] = ['g', 'G'][context.capitals]
3753
3754 # if type is '%', adjust exponent of self accordingly
3755 if spec['type'] == '%':
3756 self = _dec_from_triple(self._sign, self._int, self._exp+2)
3757
3758 # round if necessary, taking rounding mode from the context
3759 rounding = context.rounding
3760 precision = spec['precision']
3761 if precision is not None:
3762 if spec['type'] in 'eE':
3763 self = self._round(precision+1, rounding)
3764 elif spec['type'] in 'fF%':
3765 self = self._rescale(-precision, rounding)
3766 elif spec['type'] in 'gG' and len(self._int) > precision:
3767 self = self._round(precision, rounding)
3768 # special case: zeros with a positive exponent can't be
3769 # represented in fixed point; rescale them to 0e0.
3770 if not self and self._exp > 0 and spec['type'] in 'fF%':
3771 self = self._rescale(0, rounding)
3772 if not self and spec['no_neg_0'] and self._sign:
3773 adjusted_sign = 0
3774 else:
3775 adjusted_sign = self._sign
3776
3777 # figure out placement of the decimal point
3778 leftdigits = self._exp + len(self._int)
3779 if spec['type'] in 'eE':

Callers

nothing calls this directly

Calls 11

copy_absMethod · 0.95
_roundMethod · 0.95
_rescaleMethod · 0.95
getcontextFunction · 0.85
_parse_format_specifierFunction · 0.85
_format_signFunction · 0.85
strFunction · 0.85
_format_alignFunction · 0.85
_dec_from_tripleFunction · 0.85
lenFunction · 0.85
_format_numberFunction · 0.85

Tested by

no test coverage detected