1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-03 00:06:24 +00:00

add float

This commit is contained in:
Aiwantaozi
2018-10-13 08:57:50 +08:00
committed by orangedeng
parent 17430d760a
commit 1883e14f29
2 changed files with 26 additions and 0 deletions

View File

@@ -327,6 +327,8 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface
return convert.ToString(value), nil return convert.ToString(value), nil
case "int": case "int":
return convert.ToNumber(value) return convert.ToNumber(value)
case "float":
return convert.ToFloat(value)
case "password": case "password":
return convert.ToString(value), nil return convert.ToString(value), nil
case "string": case "string":

View File

@@ -105,6 +105,30 @@ func ToNumber(value interface{}) (int64, error) {
return strconv.ParseInt(ToString(value), 10, 64) 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 { func Capitalize(s string) string {
if len(s) <= 1 { if len(s) <= 1 {
return strings.ToUpper(s) return strings.ToUpper(s)