Skip to content

Commit

Permalink
Merge pull request #13 from kalaspuff/feature/sub-units-property
Browse files Browse the repository at this point in the history
Added sub_units property of money objects
  • Loading branch information
kalaspuff authored Nov 27, 2019
2 parents 4840efe + cc088fc commit 0d1fbb2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions stockholm/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ def nanos(self) -> int:
_, nanos = self._amount_tuple
return int(nanos)

@property
def sub_units(self) -> Decimal:
if self._currency and isinstance(self._currency, BaseCurrency):
if self._currency.decimal_digits == 0:
output = self._amount
else:
output = self._amount * Decimal(pow(10, self._currency.decimal_digits))
else:
output = self._amount * 100

if output == output.to_integral():
return output.to_integral()
return output

def as_string(self, *args: Any, **kwargs: Any) -> str:
amount = self.amount_as_string(*args, **kwargs)

Expand Down
31 changes: 31 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
import pytest

from stockholm import BaseCurrency, Currency, Money
Expand Down Expand Up @@ -234,30 +235,60 @@ def test_currency_types() -> None:

m = Money(4711, Currency.SEK, from_sub_units=True)
assert m == "47.11"
assert m.sub_units == 4711
m = Money(4711, Currency.SEK, from_sub_units=False)
assert m == 4711
assert m.sub_units == 471100
assert str(Money.from_sub_units(1000, Currency.SEK)) == "10.00 SEK"

m = Money(1000, Currency.JPY, from_sub_units=True)
assert m == 1000
assert m.sub_units == 1000
m = Money(1000, Currency.JPY, from_sub_units=False)
assert m == 1000
assert m.sub_units == 1000
assert str(Money.from_sub_units(1000, Currency.JPY)) == "1000 JPY"

m = Money(1000, Currency.IQD, from_sub_units=True)
assert m == 1
assert m.sub_units == 1000
m = Money(1000, Currency.IQD, from_sub_units=False)
assert m == 1000
assert m.sub_units == 1000000

m = Money(1000, Currency.CLF, from_sub_units=True)
assert m == Money("0.1")
assert m.sub_units == 1000
m = Money(1000, Currency.CLF, from_sub_units=False)
assert m == 1000
assert m.sub_units == 10000000
assert str(Money.from_sub_units(4711, Currency.CLF)) == "0.4711 CLF"

assert str(Money.from_sub_units(471100)) == "4711.00"
assert str(Money.from_sub_units(4711)) == "47.11"
assert str(Money.from_sub_units(4711, "XXX")) == "47.11 XXX"
assert Money.from_sub_units(4711).sub_units == 4711
assert Money.from_sub_units(4711, "XXX").sub_units == 4711
assert Money("1").sub_units == 100
assert Money("0.1").sub_units == 10
assert Money("0.01").sub_units == 1
assert Money("0.001").sub_units == Decimal("0.1")

assert str(Money("0.01").sub_units) == "1"
assert str(Money("0.01", currency="SEK").sub_units) == "1"
assert str(Money("100.50", currency="SEK").sub_units) == "10050"
assert "." in str(Money("100.10", Currency.JPY).sub_units)
assert str(round(Money("100.10", Currency.JPY).sub_units)) == "100"

assert Money("1", Currency.JPY).sub_units == 1
assert Money("0.1", Currency.JPY).sub_units == Decimal("0.1")
assert Money("0.01", Currency.JPY).sub_units == Decimal("0.01")
assert Money("0.001", Currency.JPY).sub_units == Decimal("0.001")

assert Money("1", Currency.SEK, from_sub_units=True).sub_units == 1
assert Money("133742", Currency.SEK, from_sub_units=True).sub_units == 133742

assert str(Money.from_sub_units(Money("133742", Currency.SEK, from_sub_units=True).to_sub_units())) == "1337.42 SEK"


def test_dogecoin():
Expand Down

0 comments on commit 0d1fbb2

Please sign in to comment.