Index: decimal.py =================================================================== --- decimal.py (revision 75309) +++ decimal.py (working copy) @@ -3549,7 +3549,7 @@ 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: + elif spec['type'] in 'gG': self = self._round(precision, rounding) # special case: zeros with a positive exponent can't be # represented in fixed point; rescale them to 0e0. @@ -5661,12 +5661,17 @@ add trailing '%' for the '%' type zero-pad if necessary fill and align if necessary + remove trailing zeros and trailing decimal point if necessary """ sign = _format_sign(is_negative, spec) - if fracpart: - fracpart = spec['decimal_point'] + fracpart + fracpart = spec['decimal_point'] + fracpart + if spec['precision'] is not None: + if spec['type'] in 'gG': + fracpart = fracpart.rstrip('0') + if fracpart == spec['decimal_point']: + fracpart = '' if exp != 0 or spec['type'] in 'eE': echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']] Index: test/test_decimal.py =================================================================== --- test/test_decimal.py (revision 75309) +++ test/test_decimal.py (working copy) @@ -775,6 +775,11 @@ # issue 6850 ('a=-7.0', '0.12345', 'aaaa0.1'), + + # issue 7098: plain g format with a precision + # should strip trailing zeros + ('.6g', '123.00', '123'), + ('g', '123.00', '123.00'), ] for fmt, d, result in test_values: self.assertEqual(format(Decimal(d), fmt), result)