Browse Source

adding Int to Float. Testing for float

tags/v1.0.0
spf13 10 years ago
parent
commit
5a9b0d4cc3
2 changed files with 18 additions and 0 deletions
  1. +8
    -0
      cast_test.go
  2. +10
    -0
      caste.go

+ 8
- 0
cast_test.go View File

@@ -21,6 +21,14 @@ func TestToInt(t *testing.T) {
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) {
var foo interface{} = "one more time"
assert.Equal(t, ToString(8), "8")


+ 10
- 0
caste.go View File

@@ -59,6 +59,16 @@ func ToFloat64E(i interface{}) (float64, bool) {
return s, true
case float32:
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:
v, err := strconv.ParseFloat(s, 64)
if err == nil {


Loading…
Cancel
Save