Skip to content

Commit

Permalink
fix round function for <1
Browse files Browse the repository at this point in the history
  • Loading branch information
BranDavidSebastian committed Jul 10, 2024
1 parent 5eda40d commit 021fae1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
19 changes: 15 additions & 4 deletions OneSila/currencies/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import math
from decimal import Decimal


def roundup(x, ceil):
''' Roundup a number to the nearest ceiling
For example: roundup(111, 20) returns 120'''
return int(math.ceil(x / Decimal(ceil))) * Decimal(ceil)
For example: roundup(111, 20) returns 120
If roundup < 1 we round up to the ceil itself
'''
ceil = Decimal(ceil)
integral_part = int(x)
fractional_part = x - integral_part

if ceil < 1:
if fractional_part < ceil:
return Decimal(integral_part) + ceil
else:
return Decimal(integral_part) + 1 + ceil
else:
return Decimal(math.ceil(x / ceil) * ceil)



def currency_convert(round_prices_up_to, exchange_rate, price):
Expand Down
11 changes: 11 additions & 0 deletions OneSila/currencies/tests/tests_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def test_currency_convert_dot_99(self):
expected_amount = 10.99
self.assertEqual(new_acount, expected_amount)

def test_currency_convert_10_dot_5_to_dot_4(self):
new_acount = float(round(
currency_convert(
round_prices_up_to=.4,
exchange_rate=1,
price=10.5,
),
2))
expected_amount = 11.4
self.assertEqual(new_acount, expected_amount)

def test_currency_convert_99(self):
new_acount = float(round(
currency_convert(
Expand Down

0 comments on commit 021fae1

Please sign in to comment.