/
formatter.go
157 lines (142 loc) · 3.88 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
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
package monetary
import (
"fmt"
)
func formatMoney(locale localeInfo, input float64) (string, error) {
value, err := floatToInt(input)
if err != nil {
return "", err
}
var buf []byte
var points int8
var grouping []int8
var decimalSymbol byte
var thousandsSymbol []byte
var currencySymbol []byte
var signSymbol []byte
var signPosition int
var currencySymbolPrecedes bool
var separatedBySpace int8
points = int8(locale.FractionalDigits)
if points < 0 || points > 10 {
points = 2 /* best guess in this case, I think */
}
// grouping here
if locale.Grouping != nil &&
len(locale.Grouping) > 0 {
grouping = locale.Grouping
} else {
grouping = []int8{3}
}
/* we restrict dsymbol to be a single byte, but not the other symbols */
if locale.DecimalPoint != 0 {
decimalSymbol = locale.DecimalPoint
} else {
decimalSymbol = decimal[0]
}
if locale.ThousandsSeparator != nil &&
len(locale.ThousandsSeparator) > 0 &&
!sliceEquals([]byte{decimalSymbol}, locale.ThousandsSeparator) {
thousandsSymbol = locale.ThousandsSeparator
} else if sliceEquals([]byte{decimalSymbol}, comma) {
thousandsSymbol = decimal
} else {
thousandsSymbol = comma
}
if locale.CurrencySymbol != nil &&
len(locale.CurrencySymbol) > 0 {
currencySymbol = locale.CurrencySymbol
} else {
currencySymbol = dollar
}
if value < 0 {
/* make the amount positive for digit-reconstruction loop */
value = -value
/* set up formatting data */
if locale.NegativeSign != nil &&
len(locale.NegativeSign) > 0 {
signSymbol = locale.NegativeSign
} else {
signSymbol = negative
}
signPosition = locale.NegativeSignPosition
currencySymbolPrecedes = locale.NegativeCurrencySymbolPrecedes
separatedBySpace = locale.NegativeSeparatedBySpace
} else {
signSymbol = locale.PositiveSign
signPosition = locale.PositiveSignPosition
currencySymbolPrecedes = locale.PositiveCurrencySymbolPrecedes
separatedBySpace = locale.PositiveSeparatedBySpace
}
digitPosition := points
for value > 0 || digitPosition >= 0 {
g := digitPosition % grouping[0]
if points > 0 && digitPosition == 0 {
buf = append([]byte{decimalSymbol}, buf...)
} else if digitPosition < 0 && g == 0 {
if len(grouping) > 1 {
prevGroup := grouping[0]
grouping = grouping[1:]
if grouping[0] == -1 {
grouping[0] = prevGroup
}
}
buf = append(thousandsSymbol, buf...)
}
buf = append([]byte{uint8((value % 10) + '0')}, buf...)
value = int64(value / 10)
digitPosition--
}
result, monetarySpace, signSpace := "", "", ""
if separatedBySpace > 0 {
monetarySpace = " "
}
if separatedBySpace == 2 {
signSpace = " "
}
switch signPosition {
case 0:
if currencySymbolPrecedes {
result = fmt.Sprintf("(%s%s%s)",
currencySymbol, monetarySpace, buf)
} else {
result = fmt.Sprintf("(%s%s%s)",
buf, monetarySpace, currencySymbol)
}
case 1:
fallthrough
default:
if currencySymbolPrecedes {
result = fmt.Sprintf("%s%s%s%s%s",
signSymbol, signSpace, currencySymbol, monetarySpace, buf)
} else {
result = fmt.Sprintf("%s%s%s%s%s",
signSymbol, signSpace, buf, monetarySpace, currencySymbol)
}
case 2:
if currencySymbolPrecedes {
result = fmt.Sprintf("%s%s%s%s%s",
currencySymbol, monetarySpace, buf, signSpace, signSymbol)
} else {
result = fmt.Sprintf("%s%s%s%s%s",
buf, monetarySpace, currencySymbol, signSpace, signSymbol)
}
case 3:
if currencySymbolPrecedes {
result = fmt.Sprintf("%s%s%s%s%s",
signSymbol, signSpace, currencySymbol, monetarySpace, buf)
} else {
result = fmt.Sprintf("%s%s%s%s%s",
buf, monetarySpace, signSymbol, signSpace, currencySymbol)
}
case 4:
if currencySymbolPrecedes {
result = fmt.Sprintf("%s%s%s%s%s",
currencySymbol, signSpace, signSymbol, monetarySpace, buf)
} else {
result = fmt.Sprintf("%s%s%s%s%s",
buf, monetarySpace, currencySymbol, signSpace, signSymbol)
}
}
return result, nil
}