diff --git a/Lib/decimal.py b/Lib/decimal.py index 4b2f3f5d1768..990afa7b015c 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -5630,9 +5630,9 @@ def _format_align(body, spec_dict): align = spec_dict['align'] if align == '<': - result = padding + sign + body - elif align == '>': result = sign + body + padding + elif align == '>': + result = padding + sign + body elif align == '=': result = sign + padding + body else: #align == '^' diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index f40de8f2078c..eaf9f0bf0c4f 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -693,6 +693,12 @@ def test_formatting(self): ('.0g', '-sNaN', '-sNaN'), ('', '1.00', '1.00'), + + # check alignment + ('<6', '123', '123 '), + ('>6', '123', ' 123'), + ('^6', '123', ' 123 '), + ('=+6', '123', '+ 123'), ] for fmt, d, result in test_values: self.assertEqual(format(Decimal(d), fmt), result) diff --git a/Misc/NEWS b/Misc/NEWS index afeb9879bf1d..bae6e78c72eb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -206,6 +206,9 @@ Core and Builtins Library ------- +- Fix Decimal.__format__ bug that swapped the meanings of the '<' and + '>' alignment characters. + - Issue #1222: locale.format() bug when the thousands separator is a space character.