105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
/* Copyright © INFINI Ltd. All rights reserved.
|
|
* web: https://infinilabs.com
|
|
* mail: hello#infini.ltd */
|
|
|
|
package funcs
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"infini.sh/framework/core/util"
|
|
"reflect"
|
|
"strconv"
|
|
)
|
|
|
|
func toFixed(precision int, num float64) float64{
|
|
return util.ToFixed(num, precision)
|
|
}
|
|
func add(a, b interface{}) float64{
|
|
av := ToFloat64(a)
|
|
bv := ToFloat64(b)
|
|
return av + bv
|
|
}
|
|
func sub(a, b interface{}) float64 { return ToFloat64(a) - ToFloat64(b) }
|
|
func div(a, b interface{}) float64 { return ToFloat64(a) / ToFloat64(b) }
|
|
func mul(a interface{}, v ...interface{}) float64 {
|
|
val := ToFloat64(a)
|
|
for _, b := range v {
|
|
val = val * ToFloat64(b)
|
|
}
|
|
return val
|
|
}
|
|
|
|
// ToFloat64 casts an interface to a float64 type.
|
|
func ToFloat64(i interface{}) float64 {
|
|
v, _ := ToFloat64E(i)
|
|
return v
|
|
}
|
|
|
|
func indirect(a interface{}) interface{} {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
|
|
// Avoid creating a reflect.Value if it's not a pointer.
|
|
return a
|
|
}
|
|
v := reflect.ValueOf(a)
|
|
for v.Kind() == reflect.Ptr && !v.IsNil() {
|
|
v = v.Elem()
|
|
}
|
|
return v.Interface()
|
|
}
|
|
|
|
// ToFloat64E casts an interface to a float64 type.
|
|
func ToFloat64E(i interface{}) (float64, error) {
|
|
i = indirect(i)
|
|
|
|
switch s := i.(type) {
|
|
case float64:
|
|
return s, nil
|
|
case float32:
|
|
return float64(s), nil
|
|
case int:
|
|
return float64(s), nil
|
|
case int64:
|
|
return float64(s), nil
|
|
case int32:
|
|
return float64(s), nil
|
|
case int16:
|
|
return float64(s), nil
|
|
case int8:
|
|
return float64(s), nil
|
|
case uint:
|
|
return float64(s), nil
|
|
case uint64:
|
|
return float64(s), nil
|
|
case uint32:
|
|
return float64(s), nil
|
|
case uint16:
|
|
return float64(s), nil
|
|
case uint8:
|
|
return float64(s), nil
|
|
case string:
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
if err == nil {
|
|
return v, nil
|
|
}
|
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
|
case json.Number:
|
|
v, err := s.Float64()
|
|
if err == nil {
|
|
return v, nil
|
|
}
|
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
|
case bool:
|
|
if s {
|
|
return 1, nil
|
|
}
|
|
return 0, nil
|
|
case nil:
|
|
return 0, nil
|
|
default:
|
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
|
}
|
|
} |