-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
75 lines (71 loc) · 1.35 KB
/
number.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
package utils
import "strconv"
// MustInt 转换值为 int 类型
func MustInt(v interface{}) int {
switch v.(type) {
case int:
return v.(int)
case int8:
return int(v.(int8))
case int16:
return int(v.(int16))
case int32:
return int(v.(int32))
case int64:
return int(v.(int64))
case float32:
return int(v.(float32))
case float64:
return int(v.(float64))
case string:
val, _ := strconv.Atoi(v.(string))
return val
}
return 0
}
// MustInt64 转换值为 int64 类型
func MustInt64(v interface{}) int64 {
switch v.(type) {
case int:
return int64(v.(int))
case int8:
return int64(v.(int8))
case int16:
return int64(v.(int16))
case int32:
return int64(v.(int32))
case int64:
return v.(int64)
case float32:
return int64(v.(float32))
case float64:
return int64(v.(float64))
case string:
val, _ := strconv.ParseInt(v.(string), 10, 64)
return val
}
return 0
}
// MustFloat64 转换值为 float64 类型
func MustFloat64(v interface{}) float64 {
switch v.(type) {
case int:
return float64(v.(int))
case int8:
return float64(v.(int8))
case int16:
return float64(v.(int16))
case int32:
return float64(v.(int32))
case int64:
return float64(v.(int64))
case float32:
return float64(v.(float32))
case float64:
return v.(float64)
case string:
val, _ := strconv.ParseFloat(v.(string), 64)
return val
}
return 0
}