forked from mirror/cast
adding Int to Float. Testing for float
This commit is contained in:
parent
d6ad1120de
commit
5a9b0d4cc3
|
@ -21,6 +21,14 @@ func TestToInt(t *testing.T) {
|
||||||
assert.Equal(t, ToInt(eight), 8)
|
assert.Equal(t, ToInt(eight), 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToFloat64(t *testing.T) {
|
||||||
|
var eight interface{} = 8
|
||||||
|
assert.Equal(t, ToFloat64(8), 8.00)
|
||||||
|
assert.Equal(t, ToFloat64(8.31), 8.31)
|
||||||
|
assert.Equal(t, ToFloat64("8.31"), 8.31)
|
||||||
|
assert.Equal(t, ToFloat64(eight), 8.0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestToString(t *testing.T) {
|
func TestToString(t *testing.T) {
|
||||||
var foo interface{} = "one more time"
|
var foo interface{} = "one more time"
|
||||||
assert.Equal(t, ToString(8), "8")
|
assert.Equal(t, ToString(8), "8")
|
||||||
|
|
10
caste.go
10
caste.go
|
@ -59,6 +59,16 @@ func ToFloat64E(i interface{}) (float64, bool) {
|
||||||
return s, true
|
return s, true
|
||||||
case float32:
|
case float32:
|
||||||
return float64(s), true
|
return float64(s), true
|
||||||
|
case int64:
|
||||||
|
return float64(s), true
|
||||||
|
case int32:
|
||||||
|
return float64(s), true
|
||||||
|
case int16:
|
||||||
|
return float64(s), true
|
||||||
|
case int8:
|
||||||
|
return float64(s), true
|
||||||
|
case int:
|
||||||
|
return float64(s), true
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseFloat(s, 64)
|
v, err := strconv.ParseFloat(s, 64)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Reference in New Issue