forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
248 lines (198 loc) · 6.41 KB
/
money.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package money
import (
"errors"
"math"
)
// Amount is a datastructure that stores the amount being used for calculations
type Amount struct {
val int64
}
// Money represents monetary value information, stores
// currency and amount value
type Money struct {
amount *Amount
currency *Currency
}
// New creates and returns new instance of Money
func New(amount float64, code string) *Money {
m := Money{
currency: newCurrency(code).get(),
}
//convert float amount to integer according minimal currency digits
m.amount = &Amount{
val: m.floatToInt(amount, m.currency.Fraction),
}
return &m
}
//internally operating with int, externally allow accept float
func (m *Money) floatToInt(f float64, precision int) int64 {
f = f * math.Pow10(precision)
f = math.Round(f)
return int64(f)
}
func (m *Money) intToFloat(i int64, precision int) float64 {
return float64(i) / math.Pow10(precision)
}
// Currency returns the currency used by Money
func (m *Money) Currency() *Currency {
return m.currency
}
// Amount returns a copy of the internal monetary value as an float64
func (m *Money) Amount() float64 {
return m.intToFloat(m.amount.val, m.currency.Fraction)
}
// SameCurrency check if given Money is equals by currency
func (m *Money) SameCurrency(om *Money) bool {
return m.currency.equals(om.currency)
}
func (m *Money) assertSameCurrency(om *Money) error {
if !m.SameCurrency(om) {
return errors.New("currencies don't match")
}
return nil
}
func (m *Money) compare(om *Money) int {
switch {
case m.amount.val > om.amount.val:
return 1
case m.amount.val < om.amount.val:
return -1
}
return 0
}
// Equals checks equality between two Money types
func (m *Money) Equals(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == 0, nil
}
// GreaterThan checks whether the value of Money is greater than the other
func (m *Money) GreaterThan(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == 1, nil
}
// GreaterThanOrEqual checks whether the value of Money is greater or equal than the other
func (m *Money) GreaterThanOrEqual(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) >= 0, nil
}
// LessThan checks whether the value of Money is less than the other
func (m *Money) LessThan(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == -1, nil
}
// LessThanOrEqual checks whether the value of Money is less or equal than the other
func (m *Money) LessThanOrEqual(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) <= 0, nil
}
// IsZero returns boolean of whether the value of Money is equals to zero
func (m *Money) IsZero() bool {
return m.amount.val == 0
}
// IsPositive returns boolean of whether the value of Money is positive
func (m *Money) IsPositive() bool {
return m.amount.val > 0
}
// IsNegative returns boolean of whether the value of Money is negative
func (m *Money) IsNegative() bool {
return m.amount.val < 0
}
// Absolute returns new Money struct from given Money using absolute monetary value
func (m *Money) Absolute() *Money {
return &Money{amount: mutate.calc.absolute(m.amount), currency: m.currency}
}
// Negative returns new Money struct from given Money using negative monetary value
func (m *Money) Negative() *Money {
return &Money{amount: mutate.calc.negative(m.amount), currency: m.currency}
}
// Add returns new Money struct with value representing sum of Self and Other Money
func (m *Money) Add(om *Money) (*Money, error) {
if err := m.assertSameCurrency(om); err != nil {
return nil, err
}
return &Money{amount: mutate.calc.add(m.amount, om.amount), currency: m.currency}, nil
}
// Subtract returns new Money struct with value representing difference of Self and Other Money
func (m *Money) Subtract(om *Money) (*Money, error) {
if err := m.assertSameCurrency(om); err != nil {
return nil, err
}
return &Money{amount: mutate.calc.subtract(m.amount, om.amount), currency: m.currency}, nil
}
// Multiply returns new Money struct with value representing Self multiplied value by multiplier
func (m *Money) Multiply(mul int64) *Money {
return &Money{amount: mutate.calc.multiply(m.amount, mul), currency: m.currency}
}
// Divide returns new Money struct with value representing Self division value by given divider
func (m *Money) Divide(div int64) *Money {
return &Money{amount: mutate.calc.divide(m.amount, div), currency: m.currency}
}
// Split returns slice of Money structs with split Self value in given number.
// After division leftover pennies will be distributed round-robin amongst the parties.
// This means that parties listed first will likely receive more pennies than ones that are listed later
func (m *Money) Split(n int) ([]*Money, error) {
if n <= 0 {
return nil, errors.New("split must be higher than zero")
}
a := mutate.calc.divide(m.amount, int64(n))
ms := make([]*Money, n)
for i := 0; i < n; i++ {
ms[i] = &Money{amount: a, currency: m.currency}
}
l := mutate.calc.modulus(m.amount, int64(n)).val
// Add leftovers to the first parties
for p := 0; l != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{1})
l--
}
return ms, nil
}
// Allocate returns slice of Money structs with split Self value in given ratios.
// It lets split money by given ratios without losing pennies and as Split operations distributes
// leftover pennies amongst the parties with round-robin principle.
func (m *Money) Allocate(rs ...int) ([]*Money, error) {
if len(rs) == 0 {
return nil, errors.New("No ratios specified")
}
// Calculate sum of ratios
var sum int
for _, r := range rs {
sum += r
}
var total int64
var ms []*Money
for _, r := range rs {
party := &Money{
amount: mutate.calc.allocate(m.amount, r, sum),
currency: m.currency,
}
ms = append(ms, party)
total += party.amount.val
}
// Calculate leftover value and divide to first parties
lo := m.amount.val - total
sub := int64(1)
if lo < 0 {
sub = -sub
}
for p := 0; lo != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{sub})
lo -= sub
}
return ms, nil
}
// Display lets represent Money struct as string in given Currency value
func (m *Money) Display() string {
c := m.currency.get()
return c.Formatter().Format(m.amount.val)
}