mirror of
https://github.com/rancher/norman.git
synced 2025-09-03 08:14:40 +00:00
add float
This commit is contained in:
@@ -327,6 +327,8 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface
|
||||
return convert.ToString(value), nil
|
||||
case "int":
|
||||
return convert.ToNumber(value)
|
||||
case "float":
|
||||
return convert.ToFloat(value)
|
||||
case "password":
|
||||
return convert.ToString(value), nil
|
||||
case "string":
|
||||
|
@@ -105,6 +105,30 @@ func ToNumber(value interface{}) (int64, error) {
|
||||
return strconv.ParseInt(ToString(value), 10, 64)
|
||||
}
|
||||
|
||||
func ToFloat(value interface{}) (float64, error) {
|
||||
value = Singular(value)
|
||||
|
||||
f64, ok := value.(float64)
|
||||
if ok {
|
||||
return f64, nil
|
||||
}
|
||||
|
||||
f32, ok := value.(float32)
|
||||
if ok {
|
||||
return float64(f32), nil
|
||||
}
|
||||
|
||||
if n, ok := value.(json.Number); ok {
|
||||
i, err := n.Int64()
|
||||
if err == nil {
|
||||
return float64(i), nil
|
||||
}
|
||||
f, err := n.Float64()
|
||||
return float64(f), err
|
||||
}
|
||||
return strconv.ParseFloat(ToString(value), 64)
|
||||
}
|
||||
|
||||
func Capitalize(s string) string {
|
||||
if len(s) <= 1 {
|
||||
return strings.ToUpper(s)
|
||||
|
Reference in New Issue
Block a user