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

Method __format__

Lib/_pydecimal.py:3725–3809  ·  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

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

Callers

nothing calls this directly

Calls 10

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
_format_numberFunction · 0.85

Tested by

no test coverage detected