-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmoney.go
282 lines (248 loc) · 6.52 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
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
277
278
279
280
281
282
// Copyright (c) 2011 Jad Dittmar
// See: https://github.com/Confunctionist/finance
//
// Some changes by Oliver Eilhard
package i18n
import (
"bytes"
"errors"
"fmt"
"math"
"strings"
)
type Money struct {
M int64
C string
}
var (
ErrMoneyOverflow = errors.New("i18n: money overflow")
ErrMoneyDivideByZero = errors.New("i18n: money division by zero")
ErrMoneyDecimalPlacesTooLarge = errors.New("i18n: money decimal places too large")
Guardi int = 100
Guard int64 = int64(Guardi)
Guardf float64 = float64(Guardi)
DP int64 = 100 // for default of 2 decimal places => 10^2 (can be reset)
DPf float64 = float64(DP) // for default of 2 decimal places => 10^2 (can be reset)
Round = .5
Roundn = Round * -1
)
const (
MAXDEC = 18
)
// Returns the absolute value of Money.
func (m *Money) Abs() *Money {
if m.M < 0 {
m.Neg()
}
return m
}
// Adds two money types.
func (m *Money) Add(n *Money) *Money {
r := m.M + n.M
if (r^m.M)&(r^n.M) < 0 {
panic(ErrMoneyOverflow)
}
m.M = r
return m
}
// Resets the package-wide decimal place (default is 2 decimal places).
func DecimalChange(d int) {
if d < 0 {
panic(ErrMoneyDivideByZero)
}
if d > MAXDEC {
panic(ErrMoneyDecimalPlacesTooLarge)
}
var newDecimal int
if d > 0 {
newDecimal++
for i := 0; i < d; i++ {
newDecimal *= 10
}
}
DPf = float64(newDecimal)
DP = int64(newDecimal)
return
}
// Divides one Money type from another.
func (m *Money) Div(n *Money) *Money {
f := Guardf * DPf * float64(m.M) / float64(n.M) / Guardf
i := int64(f)
return m.Set(Rnd(i, f-float64(i)))
}
// Gets value of money truncating after DP (see Value() for no truncation).
func (m *Money) Gett() int64 {
return m.M / DP
}
// Gets the float64 value of money (see Value() for int64).
func (m *Money) Get() float64 {
return float64(m.M) / DPf
}
// Multiplies two Money types.
func (m *Money) Mul(n *Money) *Money {
return m.Set(m.M * n.M / DP)
}
// Multiplies a Money with a float to return a money-stored type.
func (m *Money) Mulf(f float64) *Money {
i := m.M * int64(f*Guardf*DPf)
r := i / Guard / DP
return m.Set(Rnd(r, float64(i)/Guardf/DPf-float64(r)))
}
// Returns the negative value of Money.
func (m *Money) Neg() *Money {
if m.M != 0 {
m.M *= -1
}
return m
}
// Rounds int64 remainder rounded half towards plus infinity
// trunc = the remainder of the float64 calc
// r = the result of the int64 cal
func Rnd(r int64, trunc float64) int64 {
if trunc > 0 {
if trunc >= Round {
r++
}
} else {
if trunc < Roundn {
r--
}
}
return r
}
// Sets the Money field M.
func (m *Money) Set(x int64) *Money {
m.M = x
return m
}
// Sets the Money fields M and C.
func (m *Money) Setc(x int64, currency string) *Money {
m.M = x
m.C = currency
return m
}
// Sets a float64 into a Money type for precision calculations.
func (m *Money) Setf(f float64) *Money {
fDPf := f * DPf
r := int64(f * DPf)
return m.Set(Rnd(r, fDPf-float64(r)))
}
// Sets a float64 into a Money type for precision calculations.
func (m *Money) Setfc(f float64, currency string) *Money {
fDPf := f * DPf
r := int64(f * DPf)
return m.Setc(Rnd(r, fDPf-float64(r)), currency)
}
// Returns the Sign of Money 1 if positive, -1 if negative.
func (m *Money) Sign() int {
if m.M < 0 {
return -1
}
return 1
}
// String for money type representation in basic monetary unit (DOLLARS CENTS).
func (m *Money) String() string {
if m.Sign() > 0 {
return fmt.Sprintf("%d.%02d %s", m.Value()/DP, m.Value()%DP, m.C)
}
// Negative value
return fmt.Sprintf("-%d.%02d %s", m.Abs().Value()/DP, m.Abs().Value()%DP, m.C)
}
func (m *Money) Format(locale string) string {
l, found := Locales[locale]
if !found {
// If we don't have any information about the currency format,
// we'll try our best to display something useful.
return m.String()
}
// DP is a measure for decimals: 2 decimal digits => dp = 10^2
currencySymbol := m.C
curr, found := Currencies[m.C]
if found {
currencySymbol = curr.Symbol
}
// DP is a measure for decimals: 2 decimal digits => dp = 10^2
dp := int64(math.Pow10(l.CurrencyDecimalDigits))
// Group DP is a measure for grouping: 3 decimal digits => groupDp = 10^3
var groupDp int64
var groupSize int
if len(l.CurrencyGroupSizes) == 0 {
// BUG(oe): Handle currency group size
groupDp = int64(math.Pow10(3))
groupSize = 3
} else if len(l.CurrencyGroupSizes) >= 1 {
// BUG(oe): Handle currency group size
groupDp = int64(math.Pow10(l.CurrencyGroupSizes[0]))
groupSize = l.CurrencyGroupSizes[0]
}
// We use absolute values (as int64) from here on, because the
// negative sign is part of the currency format pattern.
absVal := m.Value()
if m.Sign() < 0 {
absVal = -absVal
}
wholeVal := absVal / dp
decVal := absVal % dp
// The unformatted string (without grouping and with a decimal sep of ".")
var unformatted string
if l.CurrencyDecimalDigits > 0 {
unformatted = fmt.Sprintf("%d.%0"+fmt.Sprintf("%d", l.CurrencyDecimalDigits)+"d", wholeVal, decVal)
} else {
unformatted = fmt.Sprintf("%d", wholeVal)
}
// Perform grouping operation of the whole number
// For 1234, this returns this array: [234 1]
groups := make([]string, 0)
for {
if groupDp > wholeVal {
// do not prepend zeros
groups = append(groups, fmt.Sprintf("%d", wholeVal%groupDp))
} else {
// prepend zeros
groups = append(groups, fmt.Sprintf("%0"+fmt.Sprintf("%d", groupSize)+"d", wholeVal%groupDp))
}
wholeVal /= groupDp
if wholeVal == 0 {
break
}
}
var wholeBuf bytes.Buffer
for i := range groups {
if i > 0 {
wholeBuf.WriteString(l.CurrencyGroupSeparator)
}
wholeBuf.WriteString(groups[len(groups)-i-1])
}
// Which pattern do we need?
// Notice that the minus sign is part of the pattern
var pattern string
if m.Sign() > 0 {
pattern = l.CurrencyPositivePattern
} else {
pattern = l.CurrencyNegativePattern
}
// Split into whole and decimal and build formatted number
var formatted string
parts := strings.SplitN(unformatted, ".", 2)
if len(parts) > 1 {
formatted = fmt.Sprintf("%s%s%s", wholeBuf.String(), l.CurrencyDecimalSeparator, parts[1])
} else {
formatted = wholeBuf.String()
}
output := strings.Replace(pattern, "$", currencySymbol, -1)
output = strings.Replace(output, "n", formatted, -1)
return output
}
// Subtracts one Money type from another.
func (m *Money) Sub(n *Money) *Money {
r := m.M - n.M
if (r^m.M)&^(r^n.M) < 0 {
panic(ErrMoneyOverflow)
}
m.M = r
return m
}
// Returns in int64 the value of Money (also see Gett(), See Get() for float64).
func (m *Money) Value() int64 {
return m.M
}