-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrency.py
115 lines (87 loc) · 2.59 KB
/
currency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from decimal import Decimal
class Currency:
def __init__(self, value=0.0):
if isinstance(value, str):
value = Decimal(value)
elif isinstance(value, Currency):
self.cents = value.cents
return
self.cents = int(value * 100)
def to_json(self):
# return "{}.{:0>2}".format(self.cents // 100, self.cents % 100)
return "%.2f" % (self.cents / 100)
def to_number(self):
return self.cents / 100
def __int__(self):
return self.to_json()
def __add__(self, other):
other = Currency(other)
other.cents += self.cents
other.cents = int(other.cents)
return other
__radd__ = __add__
def __iadd__(self, other):
other = Currency(other)
self.cents += other.cents
self.cents = int(self.cents)
return self
def __neg__(self):
res = Currency()
res.cents = - self.cents
return res
def __sub__(self, other):
other = Currency(other)
other.cents = self.cents - other.cents
return other
def __rsub__(self, other):
return Currency(other) - self
def __isub__(self, other):
other = Currency(other)
self.cents -= other.cents
self.cents = int(self.cents)
return self
def __mul__(self, other):
res = Currency()
res.cents = self.cents * other
res.cents = int(res.cents)
return res
__rmul__ = __mul__
def __imul__(self, other):
self.cents *= other
self.cents = int(self.cents)
return self
def __div__(self, other):
res = Currency()
res.cents = self.cents / other
res.cents = int(res.cents)
return res
def __rdiv__(self, other):
return other / self.to_number()
def __idiv__(self, other):
self.cents /= other
self.cents = int(self.cents)
return self
def __truediv__(self, other):
res = Currency()
res.cents = self.cents / other
res.cents = int(res.cents)
return res
def __rtruediv__(self, other):
return other / self.to_number()
def __floordiv__(self, other):
res = Currency()
res.cents = self.cents // other
res.cents = int(res.cents)
return res
def __rfloordiv__(self, other):
return other // self.to_number()
def __str__(self):
return self.to_json()
def __float__(self):
return self.to_number()
def test():
x = Currency(1.05)
y = Currency(0.5)
print(y - x)
if __name__ == "__main__":
test()