index.go
3.65 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package lxconv
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// IntToStr 数字转换字符串
func IntToStr(i int) string {
return strconv.Itoa(i)
}
// Int64ToStr 64位数字转换字符串
func Int64ToStr(i int64) string {
return strconv.FormatInt(i, 10)
}
// StrToInt 字符串转换位int
func StrToInt(str string) int {
intnum, _ := strconv.Atoi(str)
return intnum
}
// StrToInt64 字符串转换位int64
func StrToInt64(str string) int64 {
intnum, _ := strconv.ParseInt(str, 10, 64)
return intnum
}
// StrToFloat64 字符串转换浮点数
func StrToFloat64(str string, i int) float64 {
v2, _ := strconv.ParseFloat(str, i)
return v2
}
// ParseInt64 字符串转换位int64
func ParseInt64(val interface{}) int64 {
if val == nil {
return 0
}
switch ret := val.(type) {
case string:
return StrToInt64(ret)
case int8:
return int64(ret)
case uint8:
return int64(ret)
case int16:
return int64(ret)
case uint16:
return int64(ret)
case int:
return int64(ret)
case uint:
return int64(ret)
case int64:
return int64(ret)
case uint64:
return int64(ret)
case float32:
return int64(ret)
case float64:
return int64(ret)
}
return 0
}
// ParseInt 字符串转换位int
func ParseInt(val interface{}) int {
if val == nil {
return 0
}
switch ret := val.(type) {
case string:
return StrToInt(ret)
case int8:
return int(ret)
case uint8:
return int(ret)
case int16:
return int(ret)
case uint16:
return int(ret)
case int:
return int(ret)
case uint:
return int(ret)
case int64:
return int(ret)
case uint64:
return int(ret)
case float32:
return int(ret)
case float64:
return int(ret)
}
return 0
}
// FloatTostr 浮点数转换int卡类型
func FloatTostr(floatstr float64, i int) string {
return strconv.FormatFloat(floatstr, 'E', -1, i)
}
// JsonEncode json转字符串
func JsonEncode(data interface{}) string {
datastr, _ := json.Marshal(data)
return string(datastr)
}
// ToInt 任意类型强转int
func ToInt(a interface{}) int {
ai := 0
switch a := a.(type) {
case float64:
ai = int(a)
case float32:
ai = int(a)
case int:
ai = a
case int32:
ai = int(a)
case int64:
ai = int(a)
case string:
ai = StrToInt(a)
default:
}
return ai
}
// ToInt64 任意类型强转int64
func ToInt64(a interface{}) int64 {
var ai int64
switch a := a.(type) {
case float64:
ai = int64(a)
case float32:
ai = int64(a)
case int:
ai = int64(a)
case int32:
ai = int64(a)
case int64:
ai = a
case string:
ai = StrToInt64(a)
default:
}
return ai
}
// ToFloat64 任意类型强转float64
func ToFloat64(a interface{}) float64 {
var ai float64
switch a := a.(type) {
case float64:
ai = a
case float32:
ai = float64(a)
case int:
ai = float64(a)
case int32:
ai = float64(a)
case int64:
ai = float64(a)
case string:
ai = StrToFloat64(a, 64)
default:
}
return ai
}
// ToString 任意类型强转字符串
func ToString(a interface{}) string {
var ai string
switch a := a.(type) {
case float64:
ai = strconv.FormatFloat(a, 'E', -1, 64)
case float32:
ai = strconv.FormatFloat(float64(a), 'E', -1, 64)
case int:
ai = fmt.Sprintf("%d", a)
case int32:
ai = fmt.Sprintf("%d", a)
case int64:
ai = fmt.Sprintf("%d", a)
case string:
ai = fmt.Sprintf("%s", a)
default:
ai = fmt.Sprintf("%v", a)
}
return ai
}
func InterfaceToStr(v interface{}) string {
str := ""
switch v.(type) {
case string:
str = v.(string)
default:
jsonbyte, _ := json.Marshal(v)
str = string(jsonbyte)
}
return str
}
// ToBool 转换 bool类型
func ToBool(a interface{}) bool {
s := ToString(a)
if s == "1" || strings.ToLower(s) == "true" {
return true
}
return false
}