mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-04 14:42:47 +08:00
Feature/cover unit test (#81)
This commit is contained in:
@@ -5,105 +5,6 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func toFloat64(i any) (float64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 处理指针类型
|
||||
if reflect.TypeOf(i).Kind() == reflect.Ptr {
|
||||
if reflect.ValueOf(i).IsNil() {
|
||||
return 0, nil
|
||||
}
|
||||
i = reflect.ValueOf(i).Elem().Interface()
|
||||
}
|
||||
|
||||
switch v := i.(type) {
|
||||
case float32:
|
||||
return float64(v), nil
|
||||
case float64:
|
||||
return v, nil
|
||||
case string:
|
||||
floatValue, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return 0, ErrSyntax
|
||||
}
|
||||
return floatValue, nil
|
||||
case uint:
|
||||
return float64(v), nil
|
||||
case uint8:
|
||||
return float64(v), nil
|
||||
case uint16:
|
||||
return float64(v), nil
|
||||
case uint32:
|
||||
return float64(v), nil
|
||||
case uint64:
|
||||
return float64(v), nil
|
||||
case int:
|
||||
return float64(v), nil
|
||||
case int8:
|
||||
return float64(v), nil
|
||||
case int16:
|
||||
return float64(v), nil
|
||||
case int32:
|
||||
return float64(v), nil
|
||||
case int64:
|
||||
return float64(v), nil
|
||||
case complex64:
|
||||
return float64(real(v)), nil
|
||||
case complex128:
|
||||
return real(v), nil
|
||||
case bool:
|
||||
if v {
|
||||
return 1, nil
|
||||
} else {
|
||||
return 0, nil
|
||||
}
|
||||
//case *float32:
|
||||
// return float64(*v), nil
|
||||
//case *float64:
|
||||
// return *v, nil
|
||||
//case *string:
|
||||
// floatValue, err := strconv.ParseFloat(*v, 64)
|
||||
// if err != nil {
|
||||
// return 0, ErrSyntax
|
||||
// }
|
||||
// return floatValue, nil
|
||||
//case *uint:
|
||||
// return float64(*v), nil
|
||||
//case *uint8:
|
||||
// return float64(*v), nil
|
||||
//case *uint16:
|
||||
// return float64(*v), nil
|
||||
//case *uint32:
|
||||
// return float64(*v), nil
|
||||
//case *uint64:
|
||||
// return float64(*v), nil
|
||||
//case *int:
|
||||
// return float64(*v), nil
|
||||
//case *int8:
|
||||
// return float64(*v), nil
|
||||
//case *int16:
|
||||
// return float64(*v), nil
|
||||
//case *int32:
|
||||
// return float64(*v), nil
|
||||
//case *int64:
|
||||
// return float64(*v), nil
|
||||
//case *complex64:
|
||||
// return float64(real(*v)), nil
|
||||
//case *complex128:
|
||||
// return real(*v), nil
|
||||
//case *bool:
|
||||
// if v != nil && *v {
|
||||
// return 1, nil
|
||||
// } else {
|
||||
// return 0, nil
|
||||
// }
|
||||
default:
|
||||
return 0, ErrType
|
||||
}
|
||||
}
|
||||
|
||||
func toFloat64Reflect(i any) (float64, error) {
|
||||
if i == nil {
|
||||
return 0, nil
|
||||
|
Reference in New Issue
Block a user