-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
196 lines (176 loc) 路 3.88 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
package money
import (
"errors"
"fmt"
)
type Money struct {
amountCents int64
currency Currency
}
const (
ErrInvalidAmountStr = "invalid amount string"
ErrTypeConversion = "type conversion error"
ErrInvalidCurrency = "invalid currency"
)
type Currency string
const (
USD Currency = "USD"
EUR Currency = "EUR"
CNY Currency = "CNY"
)
// NewFromString
// @Description: NewFromString creates a new Money instance from a string.
// @Param amount
// @Param currency
func NewFromString(amount string, currency ...Currency) (*Money, error) {
// validate input
if !IsValidStrAmount(amount) {
return nil, errors.New(ErrInvalidAmountStr)
}
// get instance
m := acquireMoney()
// currency
if len(currency) > 0 {
if !isValidCurrency(string(currency[0])) {
return nil, errors.New(ErrInvalidCurrency)
}
m.currency = currency[0]
} else {
m.currency = USD
}
var err error
m.amountCents, err = parseStrToCentsInt(amount)
if err != nil {
return nil, err
}
// return
return m, nil
}
// MustFromString
// @Description: MustFromString is a helper function that wraps NewFromString and panics if an error is returned.
// @Param amount
// @Param currency
func MustFromString(amount string, currency ...Currency) *Money {
m, err := NewFromString(amount, currency...)
if err != nil {
panic(err)
}
return m
}
// AddStr
// @Description: AddStr adds a string amount to the Money instance.
// @Param str
func (m *Money) AddStr(str string) *Money {
// validate input
if !IsValidStrAmount(str) {
panic(ErrInvalidAmountStr)
}
// parse
amount, err := parseStrToCentsInt(str)
if err != nil {
panic(err)
}
// add
m.amountCents += amount
// return
return m
}
// Add
// @Description: Add adds a Money instance to the Money instance.
// @Param money
func (m *Money) Add(money *Money) *Money {
// add
m.amountCents += money.amountCents
// return
return m
}
// AddCentsInt
// @Description: AddCentsInt adds a cents int to the Money instance.
// @Param c
func (m *Money) AddCentsInt(c int) *Money {
// add
m.amountCents += int64(c)
// return
return m
}
// AddDollarInt
// @Description: AddDollarInt adds a dollar int to the Money instance.
// @Param d
func (m *Money) AddDollarInt(d int) *Money {
// add
m.amountCents += int64(d * 100)
// return
return m
}
// SubStr
// @Description: SubStr subtracts a string amount from the Money instance.
// @Param str
func (m *Money) SubStr(str string) *Money {
// validate input
if !IsValidStrAmount(str) {
panic(ErrInvalidAmountStr)
}
// parse
amount, err := parseStrToCentsInt(str)
if err != nil {
panic(err)
}
// sub
m.amountCents -= amount
// return
return m
}
// Sub
// @Description: Sub subtracts a Money instance from the Money instance.
// @Param money
func (m *Money) Sub(money *Money) *Money {
// sub
m.amountCents -= money.amountCents
// return
return m
}
// SubCentsInt
// @Description: SubCentsInt subtracts a cents int from the Money instance.
// @Param c
func (m *Money) SubCentsInt(c int) *Money {
// sub
m.amountCents -= int64(c)
// return
return m
}
// SubDollarInt
// @Description: SubDollarInt subtracts a dollar int from the Money instance.
// @Param d
func (m *Money) SubDollarInt(d int) *Money {
// sub
m.amountCents -= int64(d * 100)
// return
return m
}
// Mul
// @Description: Mul multiplies the total amount by a number.
// @Param n
func (m *Money) Mul(n int) *Money {
m.amountCents *= int64(n)
return m
}
// Div
// @Description: Div divides the total amount by a number.
// @Param n
func (m *Money) Div(n int) *Money {
m.amountCents /= int64(n)
return m
}
func (m *Money) ToString() string {
defer releaseMoney(m)
return fmt.Sprintf("%.2f", float64(m.amountCents)/100)
}
func (m *Money) ToStringCurrency() string {
defer releaseMoney(m)
return fmt.Sprintf("%s %.2f", m.currency, float64(m.amountCents)/100)
}
func (m *Money) ToCentsInt() int64 {
defer releaseMoney(m)
result := m.amountCents
return result
}