-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
83 lines (78 loc) · 1.46 KB
/
float.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
package cast
import (
"strconv"
)
// Floater is the interface that wraps the Float method.
// Float method return int64
type Floater interface {
Float() float64
}
//Float cast input to int64
func Float(input interface{}) (output float64, err error) {
switch castValue := input.(type) {
case Floater:
output = castValue.Float()
return
case string:
output, err = strconv.ParseFloat(castValue, 64)
case []byte:
output, err = strconv.ParseFloat(string(castValue), 64)
return
case int:
output = float64(castValue)
return
case int8:
output = float64(castValue)
return
case int16:
output = float64(castValue)
return
case int32:
output = float64(castValue)
return
case int64:
output = float64(castValue)
return
case uint:
output = float64(castValue)
return
case uint8:
output = float64(castValue)
return
case uint16:
output = float64(castValue)
return
case uint32:
output = float64(castValue)
return
case uint64:
output = float64(castValue)
return
case float32:
output = float64(castValue)
return
case float64:
output = float64(castValue)
return
case bool:
output = float64(0)
if castValue {
output = float64(1)
}
return
case nil:
output = float64(0)
return
default:
err = NewCastError("Could not convert to float64")
}
return
}
//MustFloat cast input to int64 and panic if error
func MustFloat(input interface{}) float64 {
output, err := Float(input)
if err != nil {
panic(err)
}
return output
}