From 8ed7570b1cc50c55867f75eafb20c1deb89dbf5c Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Thu, 25 Jan 2018 13:06:27 +0100 Subject: [FIX] tools: float_round DOWN The result of the following: ``` float_round(2.5, precision_rounding=0.05, rounding_method='DOWN') ``` is 2.45. Instead, 2.5 is expected. In case of the `DOWN` method, epsilon should be added, not subtracted. In the meantime, we introduce a slight reorganization of the method to make it clearer. opw-805008 --- odoo/addons/base/tests/test_float.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'odoo/addons/base/tests/test_float.py') diff --git a/odoo/addons/base/tests/test_float.py b/odoo/addons/base/tests/test_float.py index 725ba48365b..6dc1cda0550 100644 --- a/odoo/addons/base/tests/test_float.py +++ b/odoo/addons/base/tests/test_float.py @@ -92,6 +92,18 @@ class TestFloatPrecision(TransactionCase): try_round(1.8, '2', 0, method='UP') try_round(-1.8, '-2', 0, method='UP') + # Try some rounding value with rounding method DOWN instead of HALF-UP + # We use 2.425 because when normalizing 2.425 with precision_digits=3 it gives + # us 2424.9999999999995 as value, and if not handle correctly the rounding DOWN + # value will be incorrect (should be 2.425 and not 2.424) + try_round(2.425, '2.425', method='DOWN') + try_round(2.4249, '2.424', method='DOWN') + try_round(-2.425, '-2.425', method='DOWN') + try_round(-2.4249, '-2.424', method='DOWN') + try_round(-2.500, '-2.500', method='DOWN') + try_round(1.8, '1', 0, method='DOWN') + try_round(-1.8, '-1', 0, method='DOWN') + # Extended float range test, inspired by Cloves Almeida's test on bug #882036. fractions = [.0, .015, .01499, .675, .67499, .4555, .4555, .45555] expecteds = ['.00', '.02', '.01', '.68', '.67', '.46', '.456', '.4556'] @@ -135,8 +147,8 @@ class TestFloatPrecision(TransactionCase): try_compare(-657.4444, -657.445, 1) # Rounding to unusual rounding units (e.g. coin values) - def try_round(amount, expected, precision_rounding=None): - value = float_round(amount, precision_rounding=precision_rounding) + def try_round(amount, expected, precision_rounding=None, method='HALF-UP'): + value = float_round(amount, precision_rounding=precision_rounding, rounding_method=method) result = float_repr(value, precision_digits=2) self.assertEqual(result, expected, 'Rounding error: got %s, expected %s' % (result, expected)) @@ -145,6 +157,8 @@ class TestFloatPrecision(TransactionCase): try_round(457.3, '455.00', precision_rounding=5) try_round(457.5, '460.00', precision_rounding=5) try_round(457.1, '456.00', precision_rounding=3) + try_round(2.5, '2.50', precision_rounding=0.05, method='DOWN') + try_round(-2.5, '-2.50', precision_rounding=0.05, method='DOWN') def test_rounding_04(self): """ check that proper rounding is performed for float persistence """ -- cgit v1.2.3