[IMP] base: allow to specify decimal points with format_decimalized_number

Currently, we can format the number into decimal points with help of
the `format_decimalized_number` option of `ir.qweb.field.integer`
class. However, sometimes we need to decide how many decimal points
we need to display, which is not possible with current implementation.

This commit improves the above option and now allows to pass the number
of decimal points, with a new option `precision_digits`, which defaults
to one decimal point.

task-2792149

Part-of: odoo/odoo#87996
This commit is contained in:
MAHAMADASIF ANSARI
2022-05-04 17:46:12 +02:00
parent 5c1e7a6f5d
commit e246bf1448
2 changed files with 16 additions and 1 deletions
+2 -1
View File
@@ -142,13 +142,14 @@ class IntegerConverter(models.AbstractModel):
options = super(IntegerConverter, self).get_available_options()
options.update(
format_decimalized_number=dict(type='boolean', string=_('Decimalized number')),
precision_digits=dict(type='integer', string=_('Precision Digits')),
)
return options
@api.model
def value_to_html(self, value, options):
if options.get('format_decimalized_number'):
return tools.format_decimalized_number(value)
return tools.format_decimalized_number(value, options.get('precision_digits', 1))
return pycompat.to_text(self.user_lang().format('%d', value, grouping=True).replace(r'-', '-\N{ZERO WIDTH NO-BREAK SPACE}'))
+14
View File
@@ -42,3 +42,17 @@ class TestQwebFieldTime(common.TransactionCase):
# Only values inferior to 24 can be used
with self.assertRaises(ValueError):
self.value_to_html(24)
class TestQwebFieldInteger(common.TransactionCase):
def value_to_html(self, value, options=None):
options = options or {}
return self.env['ir.qweb.field.integer'].value_to_html(value, options)
def test_integer_value_to_html(self):
self.assertEqual(self.value_to_html(1000), "1,000")
self.assertEqual(self.value_to_html(1000000, {'format_decimalized_number': True}), "1M")
self.assertEqual(
self.value_to_html(125125, {'format_decimalized_number': True, 'precision_digits': 3}),
"125.125k"
)