-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
105 lines (96 loc) · 2.66 KB
/
formatter.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
package i18n
import (
"fmt"
"strconv"
"strings"
)
// NumberFormatter represents a number formatter.
type NumberFormatter struct {
PositivePattern string
NegativePattern string
DecimalDigits int
DecimalSeparator string
GroupSizes []int
GroupSeparator string
}
// Format formats the float value to string.
func (nf *NumberFormatter) Format(v float64) string {
value := fmt.Sprintf("%."+strconv.Itoa(nf.DecimalDigits)+"f", v)
parts := strings.Split(value, ".")
integer := parts[0]
negative := strings.HasPrefix(integer, "-")
if negative {
integer = integer[1:]
}
index := len(integer)
for i := 0; i < len(nf.GroupSizes); i++ {
size := nf.GroupSizes[i]
if size > 0 && index > size {
index -= size
integer = integer[0:index] + nf.GroupSeparator + integer[index:]
}
for i == len(nf.GroupSizes)-1 && size > 0 && index > size {
index -= size
integer = integer[0:index] + nf.GroupSeparator + integer[index:]
}
}
if len(parts) == 2 { // has decimal
value = integer + nf.DecimalSeparator + parts[1]
} else {
value = integer
}
if negative { // has pattern
value = strings.Replace(nf.NegativePattern, "n", value, -1)
} else {
value = strings.Replace(nf.PositivePattern, "n", value, -1)
}
return value
}
// CurrencyFormatter represents a currency formatter.
type CurrencyFormatter struct {
Symbol string
PositivePattern string
NegativePattern string
DecimalDigits int
DecimalSeparator string
GroupSizes []int
GroupSeparator string
}
// Format formats the float value to string.
func (cf *CurrencyFormatter) Format(v float64) string {
value := fmt.Sprintf("%."+strconv.Itoa(cf.DecimalDigits)+"f", v)
parts := strings.Split(value, ".")
integer := parts[0]
negative := strings.HasPrefix(integer, "-")
if negative {
integer = integer[1:]
}
index := len(integer)
for i := 0; i < len(cf.GroupSizes); i++ {
size := cf.GroupSizes[i]
if size > 0 && index > size {
index -= size
integer = integer[0:index] + cf.GroupSeparator + integer[index:]
}
for i == len(cf.GroupSizes)-1 && size > 0 && index > size {
index -= size
integer = integer[0:index] + cf.GroupSeparator + integer[index:]
}
}
if len(parts) == 2 { // has decimal
value = integer + cf.DecimalSeparator + parts[1]
} else {
value = integer
}
if negative { // has pattern
value = strings.Replace(strings.Replace(cf.NegativePattern, "n", value, -1), "$", cf.Symbol, -1)
} else {
value = strings.Replace(strings.Replace(cf.PositivePattern, "n", value, -1), "$", cf.Symbol, -1)
}
return value
}
// Formatter represents a formatter for number & currency.
type Formatter struct {
Number *NumberFormatter
Currency *CurrencyFormatter
}