diff --git a/cast_test.go b/cast_test.go index 6bea164..50564d3 100644 --- a/cast_test.go +++ b/cast_test.go @@ -41,6 +41,13 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64))) } + streight := eight + streightnegative := eightnegative + if kind == reflect.Float32 || kind == reflect.Float64 { + streight = eightpoint31 + streightnegative = eightpoint31negative + } + return []testStep{ {int(8), eight, false}, {int8(8), eight, false}, @@ -59,6 +66,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp {true, one, false}, {false, zero, false}, {"8", eight, false}, + {"8.31", streight, false}, {nil, zero, false}, {int(-8), eightnegative, isUint}, {int8(-8), eightnegative, isUint}, @@ -68,6 +76,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp {float32(-8.31), eightpoint31negative_32, isUint}, {float64(-8.31), eightpoint31negative, isUint}, {"-8", eightnegative, isUint}, + {"-8.31", streightnegative, isUint}, {jeight, eight, false}, {jminuseight, eightnegative, isUint}, {jfloateight, eight, false}, @@ -923,9 +932,8 @@ func TestIndirectPointers(t *testing.T) { func TestToTime(t *testing.T) { c := qt.New(t) - var jntime, jnetime json.Number + var jntime json.Number _ = json.Unmarshal([]byte("1234567890"), &jntime) - _ = json.Unmarshal([]byte("123.4567890"), &jnetime) tests := []struct { input interface{} expect time.Time @@ -968,7 +976,7 @@ func TestToTime(t *testing.T) { {time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, // errors {"2006", time.Time{}, true}, - {jnetime, time.Time{}, true}, + {nil, time.Time{}, true}, {testing.T{}, time.Time{}, true}, } diff --git a/caste.go b/caste.go index d49bbf8..ce65f76 100644 --- a/caste.go +++ b/caste.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "time" + "unicode" ) var errNegativeNotAllowed = errors.New("unable to cast negative value") @@ -281,7 +282,7 @@ func ToInt64E(i interface{}) (int64, error) { case float32: return int64(s), nil case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { return v, nil } @@ -333,7 +334,7 @@ func ToInt32E(i interface{}) (int32, error) { case float32: return int32(s), nil case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { return int32(v), nil } @@ -385,7 +386,7 @@ func ToInt16E(i interface{}) (int16, error) { case float32: return int16(s), nil case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { return int16(v), nil } @@ -437,7 +438,7 @@ func ToInt8E(i interface{}) (int8, error) { case float32: return int8(s), nil case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { return int8(v), nil } @@ -489,7 +490,7 @@ func ToIntE(i interface{}) (int, error) { case float32: return int(s), nil case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { return int(v), nil } @@ -522,7 +523,7 @@ func ToUintE(i interface{}) (uint, error) { switch s := i.(type) { case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { if v < 0 { return 0, errNegativeNotAllowed @@ -598,7 +599,7 @@ func ToUint64E(i interface{}) (uint64, error) { switch s := i.(type) { case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { if v < 0 { return 0, errNegativeNotAllowed @@ -674,7 +675,7 @@ func ToUint32E(i interface{}) (uint32, error) { switch s := i.(type) { case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { if v < 0 { return 0, errNegativeNotAllowed @@ -750,7 +751,7 @@ func ToUint16E(i interface{}) (uint16, error) { switch s := i.(type) { case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { if v < 0 { return 0, errNegativeNotAllowed @@ -826,7 +827,7 @@ func ToUint8E(i interface{}) (uint8, error) { switch s := i.(type) { case string: - v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0) + v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0) if err == nil { if v < 0 { return 0, errNegativeNotAllowed @@ -1496,3 +1497,20 @@ func trimZeroDecimal(s string) string { } return s } + +func truncateDecimals(s string) string { + _, err := strconv.ParseFloat(s, 64) + if err != nil { + return s + } + v := strings.Index(s, ".") + if v == -1 { + return s + } + for i := v + 1; i < len(s); i++ { + if !unicode.IsDigit(rune(s[i])) { + return s + } + } + return s[:v] +}