Skip to content
Snippets Groups Projects
Commit b79d8b99 authored by Nicolas Lempereur's avatar Nicolas Lempereur
Browse files

[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
parent 8d851e4a
Branches
Tags
No related merge requests found
......@@ -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
......
......@@ -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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment