-
Notifications
You must be signed in to change notification settings - Fork 9
/
currency.go
284 lines (230 loc) · 6.78 KB
/
currency.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
283
284
// Package currency helps represent a currency with high precision, and do currency computations.
package currency
import (
"errors"
"fmt"
"io"
"math"
"regexp"
"strconv"
"strings"
)
var (
// ErrMismatchCurrency is the error returned if trying to do computation between unmatched currencies
ErrMismatchCurrency = errors.New("currencies do not match")
// ErrInvalidCurrency is the error returned while trying parse an invalid currency value
ErrInvalidCurrency = errors.New("invalid currency value provided")
// ErrInvalidFUS is the error returned when Functional unit share is equal to 0
ErrInvalidFUS = errors.New("invalid functional unit share provided")
)
// replacer is the regex which replaces all invalid characters inside a string representing a currency value
var replacer = regexp.MustCompile(`([^0-9.\-\+])`)
const (
replaceWith = ""
)
// Currency represents money with all the meta data required.
type Currency struct {
// Code represents the international currency code
Code string `json:"code,omitempty"`
// Symbol is the respective currency symbol
Symbol string `json:"symbol,omitempty"`
// Main represents the main unit value of the currency
Main int `json:"main,omitempty"`
// Fractional represents the fractional unit value of the currency
Fractional int `json:"fractional,omitempty"`
// FUName is the name of the fractional unit of the currency. e.g. paise
FUName string `json:"fuName,omitempty"`
// FUShare represents the number of fractional units that make up 1 main unit. e.g. ₹1 = 100 Paise.
FUShare uint `json:"fuShare,omitempty"`
// fuDigits is the number of digits in FUShare-1 (i.e. number of digits in the maximum value which the fractional unit can have, e.g. 99 paise, 2 digits)
fuDigits int
// magnitude is the fraction which sets the magnitude required for the rounding function
magnitude float64
// PrefixSymbol if true will add the symbol as a prefix to the string representation of currency. e.g. ₹1.5
PrefixSymbol bool `json:"alwaysAddPrefix,omitempty"`
// SuffixSymbol if true will add the symbol as a suffix to the string representation of currency. e.g. 1.5₹
SuffixSymbol bool `json:"alwaysAddSuffix,omitempty"`
}
// New returns a new instance of currency.
func New(main int, fractional int, code, symbol, funame string, fushare uint) (*Currency, error) {
if fushare == 0 {
return nil, ErrInvalidFUS
}
if main != 0 && fractional < 0 {
fractional = -fractional
}
fus := int(fushare)
m := main + (fractional / fus)
f := fractional % fus
fudigits := digits(fus - 1)
mag := float64(5.0)
for i := 0; i < fudigits-1; i++ {
mag /= 10
}
return &Currency{
Code: code,
Symbol: symbol,
Main: m,
Fractional: f,
FUName: funame,
FUShare: fushare,
fuDigits: fudigits,
magnitude: mag,
}, nil
}
// NewFractional returns a new instance of currency given the total value of currency in fractional unit.
func NewFractional(ftotal int, code, symbol, funame string, fushare uint) (*Currency, error) {
if fushare == 0 {
return nil, ErrInvalidFUS
}
fus := int(fushare)
m := ftotal / fus
f := (ftotal % fus)
if m < 0 {
f = -f
}
fudigits := digits(fus - 1)
mag := float64(5.0)
for i := 0; i < fudigits-1; i++ {
mag /= 10
}
return &Currency{
Code: code,
Symbol: symbol,
Main: m,
Fractional: f,
FUName: funame,
FUShare: fushare,
fuDigits: fudigits,
magnitude: mag,
}, nil
}
// ParseString will parse a string representation of the currency and return instance of Currency.
func ParseString(value string, code, symbol, funame string, fushare uint) (*Currency, error) {
str := replacer.ReplaceAllString(value, replaceWith)
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return nil, err
}
return ParseFloat64(f, code, symbol, funame, fushare)
}
// ParseFloat64 will parse a float value into currency.
func ParseFloat64(value float64, code, symbol, funame string, fushare uint) (*Currency, error) {
fus := int(fushare)
if fus == 0 {
return nil, ErrInvalidFUS
}
fudigits := digits(fus - 1)
mag := float64(5.0)
for i := 0; i < fudigits-1; i++ {
mag /= 10
}
ftotal := round(value*float64(fushare), mag)
main := ftotal / fus
fractional := (ftotal % fus)
if main < 0 {
fractional = -fractional
}
m := main + (fractional / fus)
f := fractional % fus
return New(m, f, code, symbol, funame, fushare)
}
// FractionalTotal returns the total value in fractional int.
func (c *Currency) FractionalTotal() int {
cFrac := c.Fractional
if c.Main < 0 {
cFrac = -cFrac
}
return ((c.Main * int(c.FUShare)) + cFrac)
}
// Float64 returns the currency in float64 format.
func (c *Currency) Float64() float64 {
frac := c.Fractional
if c.Main < 0 {
frac = -frac
}
return float64(c.Main) + (float64(frac) / float64(c.FUShare))
}
func (c *Currency) StringWithoutSymbols() string {
frc := c.Fractional
if c.Fractional < 0 {
frc = -frc
}
fstr := strconv.Itoa(frc)
//all the missing digits are added to the string
for i := 0; i < c.fuDigits-len(fstr); i++ {
fstr = "0" + fstr
}
str := strconv.Itoa(c.Main) + "." + fstr
if c.Fractional < 0 {
str = "-" + str
}
return str
}
// String returns the currency represented as string.
func (c *Currency) String() string {
str := c.StringWithoutSymbols()
if c.PrefixSymbol {
if c.Fractional < 0 || c.Main < 0 {
str = strings.Replace(str, "-", "-"+c.Symbol, 1)
} else {
str = c.Symbol + str
}
}
if c.SuffixSymbol {
str = str + c.Symbol
}
return str
}
func (c *Currency) Format(s fmt.State, verb rune) {
switch verb {
// 's' verb would produce the full currency string along with its symbol. equivalent to c.String()
// 'v' - only once you add this does the fmt.Stringer seem to work, otherwise it's always printing blank
case 's', 'v':
{
_, _ = io.WriteString(s, c.String())
}
// 'd' verb would produce the main integer part of the currency, without the symbol
case 'd':
{
main := strconv.Itoa(c.Main)
_, _ = io.WriteString(s, main)
}
// 'm' verb would produce the fractional integer part of the currency, without the symbol
case 'm':
{
main := strconv.Itoa(c.Fractional)
_, _ = io.WriteString(s, main)
}
// 'f' verb would produce the full currency string without its symbol. equivalent to c.StringWithoutSymbols()
case 'f':
{
_, _ = io.WriteString(s, c.StringWithoutSymbols())
}
// 'y' verb would produce the currency symbol alone
case 'y':
{
_, _ = io.WriteString(s, c.Symbol)
}
}
}
// round rounds off the float value to the configured precision and returns an integer.
func round(f float64, magnitude float64) int {
if math.Abs(f) < 0.5 {
return 0
}
return int(f + math.Copysign(magnitude, f))
}
// digits returns the number of digits in an integer
func digits(n int) int {
if n < 0 {
n = -n
}
n /= 10
d := 1
for n > 0 {
d++
n /= 10
}
return d
}