-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods.go
276 lines (238 loc) · 8.63 KB
/
methods.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package dinerogo
import (
"errors"
"fmt"
"math"
)
// GetAmount : Get the amount of the dinero object
func (d *dinero) GetAmount() int64 {
return d.amount
}
// GetCurrency : Get currency of the Dinero Obj
func (d *dinero) GetCurrency() string {
return d.currency
}
// GetPrecision : Get the decimal precision of the Dinero obj
func (d *dinero) GetPrecision() uint8 {
return d.precision
}
//ConvertPrecision : Convert the decimal precision and the amount with a new precision
func (d *dinero) ConvertPrecision(newPrecision uint8) *dinero {
if d.GetPrecision() == newPrecision {
return d
}
if d.GetPrecision() != 0 {
return &dinero{
amount: int64(math.RoundToEven((float64(d.GetAmount()) / (math.Pow(10, float64(d.GetPrecision())))) * (math.Pow(10, float64(newPrecision))))),
currency: d.GetCurrency(),
precision: newPrecision,
}
}
return &dinero{
amount: int64(math.RoundToEven(float64(d.GetAmount()) * (math.Pow(10, float64(newPrecision))))),
currency: d.GetCurrency(),
precision: newPrecision,
}
}
// Add : Add a Dinero object to another Dinero object
func (d *dinero) Add(dinerotoAdd *dinero) (*dinero, error) {
if d.GetCurrency() != dinerotoAdd.GetCurrency() {
return &dinero{}, errors.New("dinero obj must be same currency")
}
if d.GetPrecision() == dinerotoAdd.GetPrecision() {
return &dinero{
amount: d.GetAmount() + dinerotoAdd.GetAmount(),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
if dinerotoAdd.GetPrecision() > d.GetPrecision() {
newDinero := d.ConvertPrecision(dinerotoAdd.GetPrecision())
return &dinero{
amount: dinerotoAdd.GetAmount() + newDinero.GetAmount(),
currency: d.GetCurrency(),
precision: dinerotoAdd.GetPrecision(),
}, nil
}
newDinero := dinerotoAdd.ConvertPrecision(d.GetPrecision())
return &dinero{
amount: d.GetAmount() + newDinero.GetAmount(),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
// Subtract : Subtract a Dinero object to another Dinero object
func (d *dinero) Subtract(dineroToSubtract *dinero) (*dinero, error) {
if d.GetAmount() < dineroToSubtract.GetAmount() {
return &dinero{}, errors.New("the dinero of subtract can be more than the actual")
}
if d.GetCurrency() != dineroToSubtract.GetCurrency() {
return &dinero{}, errors.New("dinero obj must be same currency")
}
if d.GetPrecision() == dineroToSubtract.GetPrecision() {
return &dinero{
amount: d.GetAmount() - dineroToSubtract.GetAmount(),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
if dineroToSubtract.GetPrecision() > d.GetPrecision() {
newDinero := d.ConvertPrecision(dineroToSubtract.GetPrecision())
return &dinero{
amount: dineroToSubtract.GetAmount() - newDinero.GetAmount(),
currency: d.GetCurrency(),
precision: dineroToSubtract.GetPrecision(),
}, nil
}
newDinero := dineroToSubtract.ConvertPrecision(d.GetPrecision())
return &dinero{
amount: d.GetAmount() - newDinero.GetAmount(),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
// Multiply : Multiply the value of a Dinero amount
func (d *dinero) Multiply(multiplier int64) *dinero {
return &dinero{
amount: int64(math.RoundToEven(float64(d.GetAmount()) * float64(multiplier))),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}
}
// Divide : Divide the value of a Dinero amount
func (d *dinero) Divide(divider int64) (*dinero, error) {
if divider == 0 {
return &dinero{}, errors.New("the divider can't be 0")
}
return &dinero{
amount: int64(math.RoundToEven(float64(d.GetAmount()) / float64(divider))),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
// Percentage : Get a Percentage of the amount of money
func (d *dinero) Percentage(percentage uint8) (*dinero, error) {
if percentage <= 0 && percentage > 100 {
return &dinero{}, errors.New("the percentage must be from 1 to 100")
}
return &dinero{
amount: int64(float64(d.GetAmount()) * float64(percentage) / 100),
currency: d.GetCurrency(),
precision: d.GetPrecision(),
}, nil
}
// EqualsTo : Compare is the Dinero object is representing the same Dinero value
func (d *dinero) EqualsTo(dineroToCompare *dinero) (bool, error) {
if d.GetCurrency() != dineroToCompare.GetCurrency() {
return false, fmt.Errorf("the can't compare %s with %s", d.GetCurrency(), dineroToCompare.GetCurrency())
}
if d.GetPrecision() != dineroToCompare.GetPrecision() {
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() == newDinero.GetAmount(), nil
}
return d.GetAmount() == dineroToCompare.GetAmount(), nil
}
// LessThan : Compare if a dinero object is less than other
func (d *dinero) LessThan(dineroToCompare *dinero) (bool, error) {
if d.GetCurrency() != dineroToCompare.GetCurrency() {
return false, fmt.Errorf("the can't compare %s with %s", d.GetCurrency(), dineroToCompare.GetCurrency())
}
if d.GetPrecision() != dineroToCompare.GetPrecision() {
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() < newDinero.GetAmount(), nil
}
return d.GetAmount() < dineroToCompare.GetAmount(), nil
}
// LessThanOrEquals : Compare if a dinero object is less than or Equals to other
func (d *dinero) LessThanOrEquals(dineroToCompare *dinero) (bool, error) {
if d.GetCurrency() != dineroToCompare.GetCurrency() {
return false, fmt.Errorf("the can't compare %s with %s", d.GetCurrency(), dineroToCompare.GetCurrency())
}
if d.GetPrecision() != dineroToCompare.GetPrecision() {
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() <= newDinero.GetAmount(), nil
}
return d.GetAmount() <= dineroToCompare.GetAmount(), nil
}
// GreatherThan : Compare if a dinero object is greather than to other
func (d *dinero) GreatherThan(dineroToCompare *dinero) (bool, error) {
if d.GetCurrency() != dineroToCompare.GetCurrency() {
return false, fmt.Errorf("the can't compare %s with %s", d.GetCurrency(), dineroToCompare.GetCurrency())
}
if d.GetPrecision() != dineroToCompare.GetPrecision() {
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() > newDinero.GetAmount(), nil
}
return d.GetAmount() > dineroToCompare.GetAmount(), nil
}
// GreatherThanOrEquals : Compare if a dinero object is greather than or Equals to other
func (d *dinero) GreatherThanOrEquals(dineroToCompare *dinero) (bool, error) {
if d.GetCurrency() != dineroToCompare.GetCurrency() {
return false, fmt.Errorf("the can't compare %s with %s", d.GetCurrency(), dineroToCompare.GetCurrency())
}
if d.GetPrecision() != dineroToCompare.GetPrecision() {
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() >= newDinero.GetAmount(), nil
}
return d.GetAmount() >= dineroToCompare.GetAmount(), nil
}
// HasSameCurrency : Compare the currency inside of the Dinero object
func (d *dinero) HasSameCurrency(dineroToCompare *dinero) bool {
return d.GetCurrency() == dineroToCompare.GetCurrency()
}
// HasSameAmount : Compare the amount inside of two Dinero object, converting to a same precision
func (d *dinero) HasSameAmount(dineroToCompare *dinero) bool {
if d.GetPrecision() == dineroToCompare.GetPrecision() {
return d.GetAmount() == dineroToCompare.GetAmount()
}
newDinero := dineroToCompare.ConvertPrecision(d.GetPrecision())
return d.GetAmount() == newDinero.GetAmount()
}
// IsZero : Valid if the amount inside of Dinero obj it's 0
func (d *dinero) IsZero() bool {
return d.GetAmount() == 0
}
// IsPositive : Valid if the amount inside of the Dinero obj it's more than 0
func (d *dinero) IsPositive() bool {
return d.GetAmount() > 0
}
// IsNegative : Valid if the amount inside of the Dinero obj it's less than 0
func (d *dinero) IsNegative() bool {
return d.GetAmount() < 0
}
// Minium : Use it for get the minimun value in a Array of Dineros
func (d *dinero) Minimun(dineros []dinero) *dinero {
var minAmount int64
var index int64
for i, dinero := range dineros {
convertedDinero := dinero.ConvertPrecision(2)
if i == 0 {
minAmount = convertedDinero.GetAmount()
} else {
if minAmount > convertedDinero.GetAmount() {
minAmount = convertedDinero.GetAmount()
index = int64(i)
}
}
}
result := dineros[index]
return &result
}
//Maximun : Use it for get the maximun value in a Array of Dineros
func (d *dinero) Maximun(dineros []dinero) *dinero {
var maxAmount int64
var index int64
for i, dinero := range dineros {
convertedDinero := dinero.ConvertPrecision(2)
if i == 0 {
maxAmount = convertedDinero.GetAmount()
} else {
if maxAmount < convertedDinero.GetAmount() {
maxAmount = convertedDinero.GetAmount()
index = int64(i)
}
}
}
result := dineros[index]
return &result
}