Skip to content

Commit

Permalink
Allow rounding with opposite method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoe committed Dec 25, 2022
1 parent eddb82a commit 458f6b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions modules/currency/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Allow rounding with opposite method

Version 6.6.0 - 2022-10-31
--------------------------
* Bug fixes (see mercurial logs for details)
Expand Down
18 changes: 16 additions & 2 deletions modules/currency/currency.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
import decimal
import logging
from decimal import ROUND_HALF_EVEN, Decimal, localcontext
from decimal import Decimal, localcontext

from dateutil.relativedelta import relativedelta
from sql import Window
Expand All @@ -24,6 +25,17 @@
logger = logging.getLogger(__name__)


ROUNDING_OPPOSITES = {
decimal.ROUND_HALF_EVEN: decimal.ROUND_HALF_EVEN,
decimal.ROUND_HALF_UP: decimal.ROUND_HALF_DOWN,
decimal.ROUND_HALF_DOWN: decimal.ROUND_HALF_UP,
decimal.ROUND_UP: decimal.ROUND_DOWN,
decimal.ROUND_DOWN: decimal.ROUND_UP,
decimal.ROUND_CEILING: decimal.ROUND_FLOOR,
decimal.ROUND_FLOOR: decimal.ROUND_CEILING,
}


class Currency(
SymbolMixin, DigitsMixin, DeactivableMixin, ModelSQL, ModelView):
'Currency'
Expand Down Expand Up @@ -148,8 +160,10 @@ def get_rate(currencies, name):
res[currency_id] = id2rate[res[currency_id]].rate
return res

def round(self, amount, rounding=ROUND_HALF_EVEN):
def round(self, amount, rounding=decimal.ROUND_HALF_EVEN, opposite=False):
'Round the amount depending of the currency'
if opposite:
rounding = ROUNDING_OPPOSITES[rounding]
return self._round(amount, self.rounding, rounding)

@classmethod
Expand Down
25 changes: 24 additions & 1 deletion modules/currency/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import datetime
import unittest
from decimal import Decimal
from decimal import ROUND_HALF_DOWN, Decimal

from trytond import backend
from trytond.modules.currency.ecb import (
Expand Down Expand Up @@ -143,6 +143,29 @@ def test_round_zero(self):

self.assertEqual(rounded, Decimal('1.2345'))

@with_transaction()
def test_round_opposite(self):
"Test the opposite rounding"
cu = create_currency('cu')
cu.save()

rounded = cu.round(Decimal('1.235'))
self.assertEqual(rounded, Decimal('1.24'))
opposite_rounded = cu.round(Decimal('1.235'), opposite=True)
self.assertEqual(opposite_rounded, Decimal('1.24'))

@with_transaction()
def test_round_opposite_HALF_DOWN(self):
"Test the oposite rounding of ROUND_HALF_DOWN"
cu = create_currency('cu')
cu.save()

rounded = cu.round(Decimal('1.235'), rounding=ROUND_HALF_DOWN)
self.assertEqual(rounded, Decimal('1.23'))
opposite_rounded = cu.round(
Decimal('1.235'), rounding=ROUND_HALF_DOWN, opposite=True)
self.assertEqual(opposite_rounded, Decimal('1.24'))

@with_transaction()
def test_is_zero(self):
"Test is zero"
Expand Down

0 comments on commit 458f6b0

Please sign in to comment.