return Amount(self.currency, abs(self.value))
def __add__(self, other):
+ if other == 0:
+ return self
if other.currency != self.currency and other.value * self.value != 0:
raise Exception("Summing amounts must be done with same currencies")
return Amount(self.currency, self.value + other.value)
raise Exception("Summing amounts must be done with same currencies")
return Amount(self.currency, self.value - other.value)
+ def __rsub__(self, other):
+ if other == 0:
+ return -self
+ else:
+ return -self.__sub__(other)
+
def __mul__(self, value):
if not isinstance(value, (int, float, D)):
raise TypeError("Amount may only be multiplied by numbers")
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 + amount4)
+ self.assertEqual(amount1, amount1 + 0)
+
def test__radd(self):
amount = portfolio.Amount("XVG", "12.9")
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 - amount4)
+ def test__rsub(self):
+ amount = portfolio.Amount("ETH", "1.6")
+ with self.assertRaises(Exception):
+ 3 - amount
+
+ self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount)
+
def test__mul(self):
amount = portfolio.Amount("XEM", 11)