From d4a987b5e7f6271ec3211cad8d5d9991a11538bb Mon Sep 17 00:00:00 2001 From: Gabriel Flores Date: Wed, 13 Jul 2022 20:50:47 -0300 Subject: [PATCH] EB-194544: Fix round in Python 3, Fix test_format_decimal_float_rounding_decimal_currency_usd_2 --- currint/currency.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/currint/currency.py b/currint/currency.py index 2257022..8402eb7 100644 --- a/currint/currency.py +++ b/currint/currency.py @@ -4,6 +4,12 @@ from decimal import Decimal +def python2round(f): + if round(f + 1) - round(f) != 1: + return f + abs(f) / f * Decimal('0.5') + return round(f) + + @six.python_2_unicode_compatible class Currency(object): """ @@ -62,7 +68,11 @@ def major_to_minor(self, value, force_round=False): # Do the maths! minor_value = value * self.divisor if force_round: - minor_value = int(round(minor_value)) + minor_value = ( + int(round(minor_value)) + if six.PY2 + else int(python2round(minor_value)) + ) if (minor_value != int(minor_value)): raise ValueError( "Cannot convert major amount %r to minor amount; would result in fractional amount of minor unit"