-
Notifications
You must be signed in to change notification settings - Fork 0
/
currencies.go
57 lines (47 loc) · 1.33 KB
/
currencies.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
package money
import (
"fmt"
"strings"
)
type currenciesSystem struct {
scaleByCode map[string]int
defaultScale int
}
var currencies currenciesSystem
func init() {
scales, err := CurrencyScalesFromISO4217(strings.NewReader(iso4217data))
if err != nil {
panic(fmt.Sprintf("reading currencies data: %s", err))
}
currencies = currenciesSystem{
scaleByCode: scales,
defaultScale: 2,
}
}
// DefaultScale is scale value used for unknown currencies.
func DefaultScale() int {
return currencies.defaultScale
}
// IsKnownCurrency checks if currency presents in ISO4217 data.
func IsKnownCurrency(curecnyCode string) bool {
_, ok := currencies.scaleByCode[curecnyCode]
return ok
}
// ScaleForCurrency gives scale value for currency code. Default scale is used for unknown currency.
func ScaleForCurrency(currencyCode string) int {
if s, ok := currencies.scaleByCode[currencyCode]; ok {
return s
}
return currencies.defaultScale
}
// ForEachCurrency is for currencies data iteration.
func ForEachCurrency(cb func(code string, scale int)) {
for c, s := range currencies.scaleByCode {
cb(c, s)
}
}
// ReplaceCurrenciesSystem should be used to replace package currencies data.
func ReplaceCurrenciesSystem(scaleByCode map[string]int, defaultScale int) {
currencies.scaleByCode = scaleByCode
currencies.defaultScale = defaultScale
}