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)
This commit is contained in:
1 parent
3671402413
commit
bb2d9765ec
1 file changed
+16
-14
@@ -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, ''),
|
||||
):
|
||||
|
||||
Reference in new issue
Block a user