-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
82 lines (70 loc) · 1.66 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
package utils
import (
"fmt"
"reflect"
"strconv"
)
type Convert struct{}
func NewConvert() *Convert {
return &Convert{}
}
// bool 转化为字符串
func (convert *Convert) BoolToString(boolValue bool) string {
if boolValue == true {
return "true"
} else {
return "false"
}
}
//bool 转化为 int
func (convert *Convert) BoolToInt(boolValue bool) int {
if boolValue == true {
return 1
} else {
return 0
}
}
//int 转化为 bool
func (convert *Convert) IntToBool(number int) bool {
if number == 0 {
return false
} else {
return true
}
}
//int 转化为字符串
//base 范围 2-32 进制
func (convert *Convert) IntToString(number int64, base int) string {
return strconv.FormatInt(number, base)
}
//string to int(10进制)
func (convert *Convert) StringToInt(str string) int {
intValue, _ := strconv.Atoi(str)
return intValue
}
// string to int64(10进制)
func (convert *Convert) StringToInt64(str string) int64 {
intValue, _ := strconv.ParseInt(str, 10, 64)
return intValue
}
// int 转化为10进制字符串 IntToString(number, 10)
func (convert *Convert) IntToTenString(number int) string {
return strconv.Itoa(number)
}
// float 转化为字符串
func (convert *Convert) FloatToString(f float64, fmt byte, prec, bitSize int) string {
return strconv.FormatFloat(f, fmt, prec, bitSize)
}
// 转化任何的数为 int64
func (convert *Convert) ToInt64(value interface{}) (d int64, err error) {
val := reflect.ValueOf(value)
switch value.(type) {
case int, int8, int16, int32, int64:
d = val.Int()
case uint, uint8, uint16, uint32, uint64:
d = int64(val.Uint())
default:
err = fmt.Errorf("ToInt64 need numeric not `%T`", value)
}
return
}