-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththousands.go
144 lines (111 loc) · 2.82 KB
/
thousands.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
// myGoToolbox is package which contains a collection of useful functions and handy tools for recurring tasks in your Go projects
package myGoToolbox
import (
"fmt"
"strconv"
"strings"
)
// Separate takes a numerical value and a language code and returns a string with thousands separators as commonly used in this lang/region. Actually supports English, German and French (intl. recommended) notation. Invalid or empty language codes let Separate return English notation as default/fallback. Scientific notation for very small or very big numbers is taken care of.
func Separate(N interface{}, lang ...string) (string, error) {
// handles all numeric types, also unsigneds (and byte and rune as they're aliases)
switch N.(type) {
case int:
case uint:
case int16:
case int32:
case int64:
case int8:
case uint16:
case uint32:
case uint64:
case uint8:
case float32:
case float64:
default:
return "", fmt.Errorf("Not a valid number!")
}
// convert to a string
n := fmt.Sprintf("%v", N)
// dealing with scientific notation
if strings.Contains(n, "e") {
nAsFlaot64, _ := strconv.ParseFloat(n, 64)
n = strconv.FormatFloat(nAsFlaot64, 'f', -1, 64)
}
// if lang omitted English is default
if len(lang) < 1 {
lang[0] = "en"
}
switch lang[0] {
case "de":
n = strings.ReplaceAll(n, ",", ".")
dec := ""
if strings.Index(n, ".") != -1 {
dec = n[strings.Index(n, ".")+1 : len(n)]
n = n[0:strings.Index(n, ".")]
}
for i := 0; i <= len(n); i = i + 4 {
a := n[0 : len(n)-i]
b := n[len(n)-i : len(n)]
n = a + "." + b
}
if n[0:1] == "." {
n = n[1:len(n)]
}
if n[len(n)-1:len(n)] == "." {
n = n[0 : len(n)-1]
}
n = strings.Trim(n, " ")
if dec != "" {
n = n + "," + dec
}
return n, nil
case "fr":
// TO-DO space as separator some more sophisticated
n = strings.ReplaceAll(n, ",", ".")
dec := ""
if strings.Index(n, ".") != -1 {
dec = n[strings.Index(n, ".")+1 : len(n)]
n = n[0:strings.Index(n, ".")]
}
for i := 0; i <= len(n); i = i + 4 {
a := n[0 : len(n)-i]
b := n[len(n)-i : len(n)]
n = a + "." + b
}
if n[0:1] == "." {
n = n[1:len(n)]
}
if n[len(n)-1:len(n)] == "." {
n = n[0 : len(n)-1]
}
if dec != "" {
n = n + "," + dec
}
// like German but exchange all "." to whitespaces. some lazy, I know :)
n = strings.ReplaceAll(n, ".", " ")
return n, nil
case "en":
n = strings.ReplaceAll(n, ",", "")
dec := ""
if strings.Index(n, ".") != -1 {
dec = n[strings.Index(n, ".")+1 : len(n)]
n = n[0:strings.Index(n, ".")]
}
for i := 0; i <= len(n); i = i + 4 {
a := n[0 : len(n)-i]
b := n[len(n)-i : len(n)]
n = a + "," + b
}
if n[0:1] == "," {
n = n[1:len(n)]
}
if n[len(n)-1:len(n)] == "," {
n = n[0 : len(n)-1]
}
if dec != "" {
n = n + "." + dec
}
return n, nil
}
return n, nil
}