-
Notifications
You must be signed in to change notification settings - Fork 6
/
money.go
118 lines (90 loc) · 2.75 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
/*
Package money is a library to deal with money and currency representation.
Inspired by ruby money library http://rubymoney.github.io/money.
Defaults
Options{
"currency": "USD",
"with_cents": true,
"with_currency": false,
"with_symbol": true,
"with_symbol_space": false,
"with_thousands_separator": true,
}
Usage
Format(10) // "$10.00"
Format(10, Options{"currency": "EUR"}) // "€10.00"
Format(10, Options{"with_cents": false}) // "$10"
Format(10, Options{"with_currency:" true }) // "$10.00 USD"
Format(10, Options{"with_symbol": false}) // "10.00"
Format(10, Options{"with_symbol_space":true}) // "$ 10.00"
Format(1000) // "$1,000.00"
Format(1000, Options{"with_thousands_separator": false}) // "$1000.00"
*/
package money
import (
"fmt"
"math"
"strings"
)
// Format returns a formatted price string according to currency rules and options
func Format(val float64, opts ...Options) (result string) {
options := defaults()
if len(opts) > 0 {
options = override(options, opts[0])
}
c := currencies[options["currency"].(string)]
integer, fractional := splitValue(val)
if options["with_thousands_separator"].(bool) {
result = separateThousands(integer, c.ThousandsSeparator)
} else {
result = integer
}
if options["with_cents"].(bool) && c.SubUnit != "" {
result = fmt.Sprintf("%s%s%s", result, c.DecimalMark, fractional)
}
if options["with_symbol"].(bool) {
result = addSymbol(result, c, options)
}
if options["with_currency"].(bool) {
result = fmt.Sprintf("%s %s", result, options["currency"].(string))
}
return result
}
func addSymbol(result string, c currency, options Options) string {
var space string
if options["with_symbol_space"].(bool) {
space = " "
}
if c.SymbolFirst {
result = fmt.Sprintf("%s%s%s", c.Symbol, space, result)
} else {
result = fmt.Sprintf("%s%s%s", result, space, c.Symbol)
}
return result
}
func separateThousands(value, separator string) string {
chunks := len(value) / 3
if chunks == 0 {
return value
}
if partial := math.Mod(float64(len(value)), 3); partial > 0 {
chunks++
}
result := make([]string, chunks)
for i := chunks - 1; i >= 0; i-- {
if i == 0 {
result[i] = value
break
}
chunk := value[len(value)-3:]
value = strings.TrimSuffix(value, chunk)
result[i] = chunk
}
return strings.Join(result, separator)
}
func splitValue(val float64) (integer, fractional string) {
i, f := math.Modf(val)
integer = fmt.Sprintf("%.0f", i)
fractional = fmt.Sprintf("%.2f", f)[2:]
return
}