forked from swxctx/gutil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.go
95 lines (85 loc) · 1.69 KB
/
convert.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
package gutil
import (
"fmt"
"strconv"
)
// StringToInt32
func StringToInt32(s string) int32 {
i, _ := strconv.ParseInt(s, 10, 64)
return int32(i)
}
// Int32ToString
func Int32ToString(i int32) string {
return strconv.FormatInt(int64(i), 10)
}
// Int32ToStringSlice
func Int32ToStringSlice(a []int32) []string {
r := make([]string, len(a))
for k, v := range a {
r[k] = Int32ToString(v)
}
return r
}
// StringToInt32Slice
func StringToInt32Slice(a []string) []int32 {
r := make([]int32, len(a))
for k, v := range a {
r[k] = StringToInt32(v)
}
return r
}
/*
input: [1,2,3,4]
output: 1,2,3,4
*/
// GetInt64SliceToString int64数组转为string
func GetInt64SliceToString(list []int64) (s string) {
n := len(list)
for i := 0; i < len(list); i++ {
s += fmt.Sprintf("%v", list[i])
if i < n-1 {
s += ","
}
}
return
}
// StringSiceToInt64 string数组转[]int64
func StringSiceToInt64(str []string) []int64 {
var (
ids []int64
)
for _, i := range str {
id, err := strconv.ParseInt(i, 10, 64)
if err != nil {
continue
}
ids = append(ids, id)
}
return ids
}
// Int64ToString int64->string
func Int64ToString(n int64) string {
return strconv.FormatInt(n, 10)
}
/*
input: 12.3455
output: 12
*/
// Float32ToInt64 float32转为int64
func Float32ToInt64(count float32) (int64, error) {
countStr := fmt.Sprintf("%0.0f", count)
return strconv.ParseInt(countStr, 10, 64)
}
// InterfaceToString interface转换成string
func InterfaceToString(interf interface{}) string {
rs := ""
switch interf.(type) {
case int, int32, int64:
rs = fmt.Sprintf("%d", interf)
case float32, float64:
rs = fmt.Sprintf("%.0f", interf)
case string:
rs = fmt.Sprintf("%s", interf)
}
return rs
}