-
Notifications
You must be signed in to change notification settings - Fork 0
/
val.go
86 lines (72 loc) · 1.38 KB
/
val.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
package util
import (
"encoding/json"
"errors"
"strconv"
)
func ToJsonString(v interface{}) (string, error) {
bt, err := json.Marshal(v)
return string(bt), err
}
func FloatToStr(v float64) string {
return strconv.FormatFloat(v, 'f', -1, 64)
}
func IntToStr(v int) string {
return strconv.Itoa(v)
}
func Int64ToStr(v int64) string {
return strconv.FormatInt(v, 10)
}
func Uint64ToStr(v uint64) string {
return strconv.FormatUint(v, 10)
}
func Uint8ToStr(v uint8) string {
return strconv.FormatUint(uint64(v), 10)
}
func FloatToInt(v float64) int {
return int(v)
}
func IntToFloat(v int) float64 {
return float64(v)
}
func StrToInt(v string) (int, error) {
if v == "" {
return 0, nil
}
res, err := strconv.Atoi(v)
return res, err
}
func StrToUint8(v string) (uint8, error) {
if v == "" {
return 0, nil
}
res, err := strconv.Atoi(v)
if err != nil {
return 0, err
}
if res < 0 || res > 255 {
return 0, errors.New("[error] 超出范围")
}
return uint8(res), err
}
func StrToUint64(v string) (uint64, error) {
if v == "" {
return 0, nil
}
res, err := strconv.ParseUint(v, 10, 64)
return res, err
}
func StrToInt64(v string) (int64, error) {
if v == "" {
return 0, nil
}
res, err := strconv.ParseInt(v, 10, 64)
return res, err
}
func StrToFloat(v string) (float64, error) {
if v == "" {
return 0, nil
}
res, err := strconv.ParseFloat(v, 64)
return res, err
}