github.com/sandwich-go/boost@v1.3.29/xconv/float.go (about)

     1  package xconv
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // Float32 converts `any` to float32.
     8  func Float32(any interface{}) float32 {
     9  	if any == nil {
    10  		return 0
    11  	}
    12  	switch value := any.(type) {
    13  	case float32:
    14  		return value
    15  	case float64:
    16  		return float32(value)
    17  	case []byte:
    18  		return LittleEndianDecodeToFloat32(value)
    19  	default:
    20  		if f, ok := value.(iFloat32); ok {
    21  			return f.Float32()
    22  		}
    23  		v, _ := strconv.ParseFloat(String(any), 32)
    24  		return float32(v)
    25  	}
    26  }
    27  
    28  // Float64 [影响性能] converts `any` to float64.
    29  func Float64(any interface{}) float64 {
    30  	if any == nil {
    31  		return 0
    32  	}
    33  	switch value := any.(type) {
    34  	case float32:
    35  		return float64(value)
    36  	case float64:
    37  		return value
    38  	case []byte:
    39  		return LittleEndianDecodeToFloat64(value)
    40  	default:
    41  		if f, ok := value.(iFloat64); ok {
    42  			return f.Float64()
    43  		}
    44  		v, _ := strconv.ParseFloat(String(any), 64)
    45  		return v
    46  	}
    47  }