adding Int to Float. Testing for float

This commit is contained in:
spf13 2014-04-07 11:43:25 -04:00
parent d6ad1120de
commit 5a9b0d4cc3
2 changed files with 18 additions and 0 deletions

View File

@ -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")

View File

@ -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 {