Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
AspieSoft committed Jan 18, 2023
1 parent 482cef0 commit 45202f2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,41 @@ func ToInt(res interface{}) int {
}
}

// ToFloat converts multiple types to a float64
//
// accepts: int, int32, int64, float64, float32, string, []byte, byte
func ToFloat(res interface{}) float64 {
switch reflect.TypeOf(res) {
case VarType["int"]:
return float64(res.(int))
case VarType["int32"]:
return float64(res.(int32))
case VarType["int64"]:
return float64(res.(int64))
case VarType["float64"]:
return float64(res.(float64))
case VarType["float32"]:
return float64(res.(float32))
case VarType["string"]:
if i, err := strconv.ParseFloat(res.(string), 64); err == nil {
return i
}
return 0
case VarType["byteArray"]:
if i, err := strconv.ParseFloat(string(res.([]byte)), 64); err == nil {
return i
}
return 0
case VarType["byte"]:
if i, err := strconv.ParseFloat(string(res.(byte)), 64); err == nil {
return i
}
return 0
default:
return 0
}
}

// IsZeroOfUnderlyingType can be used to determine if an interface{} in null or empty
func IsZeroOfUnderlyingType(x interface{}) bool {
// return x == nil || x == reflect.Zero(reflect.TypeOf(x)).Interface()
Expand Down

0 comments on commit 45202f2

Please sign in to comment.