changeset 1e50ad7b9fa6 in tryton:5.8
details: https://hg.tryton.org/tryton?cmd=changeset;node=1e50ad7b9fa6
description:
        Support float represented by exponential

        The domain parser need to find the number of digit to display by 
analysing the
        string format of the value. Some small values may be formatted as 
exponential.

        issue9955
        review347261002
        (grafted from a8eec0bd53ed6c82579f3c34e9752d300c1b0f0a)
diffstat:

 tryton/common/domain_parser.py            |  11 +++++++----
 tryton/tests/test_common_domain_parser.py |   2 ++
 2 files changed, 9 insertions(+), 4 deletions(-)

diffs (38 lines):

diff -r 1b1208051d0e -r 1e50ad7b9fa6 tryton/common/domain_parser.py
--- a/tryton/common/domain_parser.py    Mon Jan 11 00:08:50 2021 +0100
+++ b/tryton/common/domain_parser.py    Mon Jan 18 23:29:44 2021 +0100
@@ -313,15 +313,18 @@
                 and (not isinstance(value, (int, float, Decimal))
                     or isinstance(value, bool))):
             return ''
+        digit = 0
         if isinstance(value, Decimal):
             cast = Decimal
         else:
             cast = float
         factor = cast(field.get('factor', 1))
-        try:
-            digit = len(str(value * factor).rstrip('0').split('.')[1])
-        except IndexError:
-            digit = 0
+        string_ = str(value * factor)
+        if 'e' in string_:
+            string_, exp = string_.split('e')
+            digit -= int(exp)
+        if '.' in string_:
+            digit += len(string_.rstrip('0').split('.')[1])
         return locale.localize(
             '{0:.{1}f}'.format(value * factor or 0, digit), True)
 
diff -r 1b1208051d0e -r 1e50ad7b9fa6 tryton/tests/test_common_domain_parser.py
--- a/tryton/tests/test_common_domain_parser.py Mon Jan 11 00:08:50 2021 +0100
+++ b/tryton/tests/test_common_domain_parser.py Mon Jan 18 23:29:44 2021 +0100
@@ -288,6 +288,8 @@
                 (0.0, '0'),
                 (False, ''),
                 (None, ''),
+                (1e-12, '0.000000000001'),
+                (1.0579e-10, '0.00000000010579'),
                 ):
             self.assertEqual(
                 format_value(field, value), result,

Reply via email to