4 def __init__(self, amount="0"):
6 if isinstance(amount, money):
8 self.amount = decimal.Decimal(amount)
9 except decimal.InvalidOperation:
10 raise ValueError("amount value could not be converted to "
11 "Decimal(): '{}'".format(amount))
17 return hash(self.amount)
20 return "{}".format(self.amount)
23 return self.__unicode__().encode('utf-8')
25 def __unicode__(self):
26 return u"${:,.2f}".format(self.amount)
28 def __lt__(self, other):
29 if not isinstance(other, money):
31 return self.amount < other.amount
33 def __le__(self, other):
34 if not isinstance(other, money):
36 return self.amount <= other.amount
38 def __eq__(self, other):
39 if isinstance(other, money):
40 return self.amount == other.amount
43 def __ne__(self, other):
44 return not self == other
46 def __gt__(self, other):
47 if not isinstance(other, money):
49 return self.amount > other.amount
51 def __ge__(self, other):
52 if not isinstance(other, money):
54 return self.amount >= other.amount
56 def __add__(self, other):
57 if not isinstance(other, money):
60 amount = self.amount + other
61 return self.__class__(amount)
63 def __radd__(self, other):
64 return self.__add__(other)
66 def __sub__(self, other):
67 if not isinstance(other, money):
70 amount = self.amount - other
71 return self.__class__(amount)
73 def __rsub__(self, other):
74 return (-self).__add__(other)
76 def __mul__(self, other):
77 if isinstance(other, money):
78 other = other.amount()
79 amount = self.amount * decimal.Decimal(other)
80 return self.__class__(amount)
82 def __rmul__(self, other):
83 return self.__mul__(other)
85 def __div__(self, other):
86 return self.__truediv__(other)
88 def __truediv__(self, other):
89 if isinstance(other, money):
91 raise ZeroDivisionError()
92 return self.amount / other.amount
95 raise ZeroDivisionError()
96 amount = self.amount / decimal.Decimal(other)
97 return self.__class__(amount)
99 def __floordiv__(self, other):
100 if isinstance(other, money):
101 if other.amount == 0:
102 raise ZeroDivisionError()
103 return self.amount // other.amount
106 raise ZeroDivisionError()
107 amount = self.amount // other
108 return self.__class__(amount)
110 def __mod__(self, other):
111 if isinstance(other, money):
112 raise TypeError("modulo is unsupported between two '{}' "
113 "objects".format(self.__class__.__name__))
115 raise ZeroDivisionError()
116 amount = self.amount % other
117 return self.__class__(amount)
119 def __divmod__(self, other):
120 if isinstance(other, money):
121 if other.amount == 0:
122 raise ZeroDivisionError()
123 return divmod(self.amount, other.amount)
126 raise ZeroDivisionError()
127 whole, remainder = divmod(self.amount, other)
128 return (self.__class__(whole),
129 self.__class__(remainder))
131 def __pow__(self, other):
132 if isinstance(other, money):
133 raise TypeError("power operator is unsupported between two '{}' "
134 "objects".format(self.__class__.__name__))
135 amount = self.amount ** other
136 return self.__class__(amount)
139 return self.__class__(-self.amount)
142 return self.__class__(+self.amount)
145 return self.__class__(abs(self.amount))
148 return int(self.amount)
151 return float(self.amount)
153 def __round__(self, ndigits=0):
154 return self.__class__(round(self.amount, ndigits))