'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 defaults to \'g\' or \'G\', depending on the
value of context.capitals.'

def __format__(self, specifier, context=None, _localeconv=None):

    if (context is None):
        context = getcontext()
    spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
    if self._is_special:
        sign = _format_sign(self._sign, spec)
        body = str(self.copy_abs())
        return _format_align(sign, body, spec)
    if (spec['type'] is None):
        spec['type'] = ['g', 'G'][context.capitals]
    if (spec['type'] == '%'):
        self = _dec_from_triple(self._sign, self._int, (self._exp + 2))
    rounding = context.rounding
    precision = spec['precision']
    if (precision is not None):
        if (spec['type'] in 'eE'):
            self = self._round((precision + 1), rounding)
        elif (spec['type'] in 'fF%'):
            self = self._rescale((- precision), rounding)
        elif ((spec['type'] in 'gG') and (len(self._int) > precision)):
            self = self._round(precision, rounding)
    if ((not self) and (self._exp > 0) and (spec['type'] in 'fF%')):
        self = self._rescale(0, rounding)
    leftdigits = (self._exp + len(self._int))
    if (spec['type'] in 'eE'):
        if ((not self) and (precision is not None)):
            dotplace = (1 - precision)
        else:
            dotplace = 1
    elif (spec['type'] in 'fF%'):
        dotplace = leftdigits
    elif (spec['type'] in 'gG'):
        if ((self._exp <= 0) and (leftdigits > (-6))):
            dotplace = leftdigits
        else:
            dotplace = 1
    if (dotplace < 0):
        intpart = '0'
        fracpart = (('0' * (- dotplace)) + self._int)
    elif (dotplace > len(self._int)):
        intpart = (self._int + ('0' * (dotplace - len(self._int))))
        fracpart = ''
    else:
        intpart = (self._int[:dotplace] or '0')
        fracpart = self._int[dotplace:]
    exp = (leftdigits - dotplace)
    return _format_number(self._sign, intpart, fracpart, exp, spec)
