From bb2d9765ece716237d4a4c9d40c679b9b7234a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 4 Sep 2013 10:41:21 +0200 Subject: [PATCH] Don't use field digits to format float/Decimal in domain_parser The conversion of a domain into string should keep the same significant digit, which means to not use the field digits definition but significant digit of the value. issue3266 review1041002 (transplanted from add046625334e0237bc6fc9d684427e6de1b1903) --- tryton/common/domain_parser.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tryton/common/domain_parser.py b/tryton/common/domain_parser.py index 2a136c9e38..bbe8011820 100644 --- a/tryton/common/domain_parser.py +++ b/tryton/common/domain_parser.py @@ -393,10 +393,10 @@ def format_value(field, value): and not isinstance(value, (float, Decimal))): return '' try: - digit = int(field.get('digits', (16, 2))[1]) - except ValueError: - digit = 2 - return locale.format('%.*f', (digit, value or 0.0), True) + digit = len(str(value).split('.')[1]) + except IndexError: + digit = 0 + return locale.format('%.*f', (digit, value or 0), True) def format_selection(): selections = dict(field['selection']) @@ -474,13 +474,14 @@ def test_format_integer(): def test_format_float(): field = { 'type': 'float', - 'digits': (16, 2), } for value, result in ( - (1, '1.00'), - (1.5, '1.50'), - (0, '0.00'), - (0.0, '0.00'), + (1, '1'), + (1.5, '1.5'), + (1.50, '1.5'), + (150.79, '150.79'), + (0, '0'), + (0.0, '0.0'), (False, ''), (None, ''), ): @@ -490,13 +491,14 @@ def test_format_float(): def test_format_numeric(): field = { 'type': 'numeric', - 'digits': (16, 2), } for value, result in ( - (Decimal(1), '1.00'), - (Decimal('1.5'), '1.50'), - (Decimal(0), '0.00'), - (Decimal('0.0'), '0.00'), + (Decimal(1), '1'), + (Decimal('1.5'), '1.5'), + (Decimal('1.50'), '1.50'), + (Decimal('150.79'), '150.79'), + (Decimal(0), '0'), + (Decimal('0.0'), '0.0'), (False, ''), (None, ''), ):