summaryrefslogtreecommitdiff
path: root/odoo/tools/float_utils.py
diff options
context:
space:
mode:
authorqdp-odoo <qdp@odoo.com>2017-03-03 15:58:05 +0100
committerqdp-odoo <qdp@odoo.com>2017-03-03 15:58:05 +0100
commit0031f98f1d469a0fcece776ec1d510db374bffa5 (patch)
tree0b28f6073ccdeac2083a7da4d37c5bed2cc9a7dc /odoo/tools/float_utils.py
parenteb56b9bebc56dbcde8d7d1fad09aeae331de7c8f (diff)
[ADD] tools/float_utils: add float_split() and float_split_str() methods.
Add utility methods allowing to split floating points numbers and return their unitary and decimal parts.
Diffstat (limited to 'odoo/tools/float_utils.py')
-rw-r--r--odoo/tools/float_utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/odoo/tools/float_utils.py b/odoo/tools/float_utils.py
index 67777145dba..127c546125f 100644
--- a/odoo/tools/float_utils.py
+++ b/odoo/tools/float_utils.py
@@ -150,6 +150,32 @@ def float_repr(value, precision_digits):
_float_repr = float_repr
+def float_split_str(value, precision_digits):
+ """ Splits the given float 'value' in its unitary and decimal parts. The value
+ is first rounded thanks to the ``precision_digits`` argument given.
+
+ Example: 1.432 would return (1, 43) for a digits precision of 2.
+
+ :param float value: value to split.
+ :param int precision_digits: number of fractional digits to round to.
+ :return: returns the tuple(<unitary part>, <decimal part>) of the given value
+ :rtype: tuple(str, str)
+ """
+ value = float_round(value, precision_digits=precision_digits)
+ value_repr = float_repr(value, precision_digits)
+ units, cents = value_repr.split('.')
+ return units, cents
+
+def float_split(value, precision_digits):
+ """ same as float_split_str() except that it returns the unitary and decimal
+ parts as integers instead of strings.
+
+ :rtype: tuple(int, int)
+ """
+ units, cents = float_split_str(value, precision_digits)
+ return int(units), int(cents)
+
+
class float_precision(float):
""" A class for float values that carry precision digits. This is a thin
layer on top of ``float``, and the precision digits are not propagated