diff options
| author | Nicolas Lempereur <nle@odoo.com> | 2018-04-10 12:39:58 +0200 |
|---|---|---|
| committer | Nicolas Lempereur <nle@odoo.com> | 2018-04-11 08:57:20 +0200 |
| commit | b79d8b99ff39d1889df89debed012fc7cc54598e (patch) | |
| tree | 1bf8b06ba89a6cec629a2113c278c76f4863fd34 | |
| parent | 8d851e4a17ca41f6a00a3937b789f04240a67c59 (diff) | |
[FIX] base: amount_to_text possible difference
In 79038055 amount_to_text was refactored and improved, but there is a
drawback.
ie. if we have .29 cents with two decimals, `Twenty-Eight` was displayed
because `int(0.29 * 100) == 28` due to floating-point representation.
This commit keeps the improvements and fix this issue by using string
formatting of float as was done before.
opw-1829924
closes #24161
| -rw-r--r-- | odoo/addons/base/res/res_currency.py | 13 | ||||
| -rw-r--r-- | odoo/addons/base/tests/test_float.py | 9 |
2 files changed, 17 insertions, 5 deletions
diff --git a/odoo/addons/base/res/res_currency.py b/odoo/addons/base/res/res_currency.py index fbb5fac2aaa..1958172ab24 100644 --- a/odoo/addons/base/res/res_currency.py +++ b/odoo/addons/base/res/res_currency.py @@ -101,17 +101,20 @@ class Currency(models.Model): logging.getLogger(__name__).warning("The library 'num2words' is missing, cannot render textual amounts.") return "" - fractional_value, integer_value = math.modf(amount) - fractional_amount = round(abs(fractional_value), self.decimal_places) * (math.pow(10, self.decimal_places)) + formatted = "%.{0}f".format(self.decimal_places) % amount + parts = formatted.partition('.') + integer_value = int(parts[0]) + fractional_value = int(parts[2] or 0) + lang_code = self.env.context.get('lang') or self.env.user.lang lang = self.env['res.lang'].search([('code', '=', lang_code)]) amount_words = tools.ustr('{amt_value} {amt_word}').format( - amt_value=_num2words(int(integer_value), lang=lang.iso_code), + amt_value=_num2words(integer_value, lang=lang.iso_code), amt_word=self.currency_unit_label, ) - if not self.is_zero(fractional_value): + if not self.is_zero(amount - integer_value): amount_words += ' ' + _('and') + tools.ustr(' {amt_value} {amt_word}').format( - amt_value=_num2words(int(fractional_amount), lang=lang.iso_code), + amt_value=_num2words(fractional_value, lang=lang.iso_code), amt_word=self.currency_subunit_label, ) return amount_words diff --git a/odoo/addons/base/tests/test_float.py b/odoo/addons/base/tests/test_float.py index 6dc1cda0550..c55ba2588f3 100644 --- a/odoo/addons/base/tests/test_float.py +++ b/odoo/addons/base/tests/test_float.py @@ -203,3 +203,12 @@ class TestFloatPrecision(TransactionCase): with self.assertRaises(AssertionError): float_round(0.01, precision_digits=3, precision_rounding=0.01) + + def test_amount_to_text_10(self): + """ verify that amount_to_text works as expected """ + currency = self.env.ref('base.EUR') + + amount_target = currency.amount_to_text(0.29) + amount_test = currency.amount_to_text(0.28) + self.assertNotEqual(amount_test, amount_target, + "Amount in text should not depend on float representation") |
