MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / __format__

Method __format__

tools/python-3.11.9-amd64/Lib/_pydecimal.py:3758–3842  ·  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

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

Source from the content-addressed store, hash-verified

3756 # PEP 3101 support. the _localeconv keyword argument should be
3757 # considered private: it's provided for ease of testing only.
3758 def __format__(self, specifier, context=None, _localeconv=None):
3759 """Format a Decimal instance according to the given specifier.
3760
3761 The specifier should be a standard format specifier, with the
3762 form described in PEP 3101. Formatting types 'e', 'E', 'f',
3763 'F', 'g', 'G', 'n' and '%' are supported. If the formatting
3764 type is omitted it defaults to 'g' or 'G', depending on the
3765 value of context.capitals.
3766 """
3767
3768 # Note: PEP 3101 says that if the type is not present then
3769 # there should be at least one digit after the decimal point.
3770 # We take the liberty of ignoring this requirement for
3771 # Decimal---it's presumably there to make sure that
3772 # format(float, '') behaves similarly to str(float).
3773 if context is None:
3774 context = getcontext()
3775
3776 spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
3777
3778 # special values don't care about the type or precision
3779 if self._is_special:
3780 sign = _format_sign(self._sign, spec)
3781 body = str(self.copy_abs())
3782 if spec['type'] == '%':
3783 body += '%'
3784 return _format_align(sign, body, spec)
3785
3786 # a type of None defaults to 'g' or 'G', depending on context
3787 if spec['type'] is None:
3788 spec['type'] = ['g', 'G'][context.capitals]
3789
3790 # if type is '%', adjust exponent of self accordingly
3791 if spec['type'] == '%':
3792 self = _dec_from_triple(self._sign, self._int, self._exp+2)
3793
3794 # round if necessary, taking rounding mode from the context
3795 rounding = context.rounding
3796 precision = spec['precision']
3797 if precision is not None:
3798 if spec['type'] in 'eE':
3799 self = self._round(precision+1, rounding)
3800 elif spec['type'] in 'fF%':
3801 self = self._rescale(-precision, rounding)
3802 elif spec['type'] in 'gG' and len(self._int) > precision:
3803 self = self._round(precision, rounding)
3804 # special case: zeros with a positive exponent can't be
3805 # represented in fixed point; rescale them to 0e0.
3806 if not self and self._exp > 0 and spec['type'] in 'fF%':
3807 self = self._rescale(0, rounding)
3808 if not self and spec['no_neg_0'] and self._sign:
3809 adjusted_sign = 0
3810 else:
3811 adjusted_sign = self._sign
3812
3813 # figure out placement of the decimal point
3814 leftdigits = self._exp + len(self._int)
3815 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