Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion currint/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"
Expand Down