github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_float.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"reflect"
    11  	"strconv"
    12  
    13  	"github.com/gogf/gf/v2/encoding/gbinary"
    14  	"github.com/gogf/gf/v2/util/gconv/internal/localinterface"
    15  )
    16  
    17  // Float32 converts `any` to float32.
    18  func Float32(any interface{}) float32 {
    19  	if any == nil {
    20  		return 0
    21  	}
    22  	switch value := any.(type) {
    23  	case float32:
    24  		return value
    25  	case float64:
    26  		return float32(value)
    27  	case []byte:
    28  		// TODO: It might panic here for these types.
    29  		return gbinary.DecodeToFloat32(value)
    30  	default:
    31  		rv := reflect.ValueOf(any)
    32  		switch rv.Kind() {
    33  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    34  			return float32(rv.Int())
    35  		case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    36  			return float32(rv.Uint())
    37  		case reflect.Float32, reflect.Float64:
    38  			return float32(rv.Float())
    39  		case reflect.Bool:
    40  			if rv.Bool() {
    41  				return 1
    42  			}
    43  			return 0
    44  		case reflect.String:
    45  			f, _ := strconv.ParseFloat(rv.String(), 32)
    46  			return float32(f)
    47  		case reflect.Ptr:
    48  			if rv.IsNil() {
    49  				return 0
    50  			}
    51  			if f, ok := value.(localinterface.IFloat32); ok {
    52  				return f.Float32()
    53  			}
    54  			return Float32(rv.Elem().Interface())
    55  		default:
    56  			if f, ok := value.(localinterface.IFloat32); ok {
    57  				return f.Float32()
    58  			}
    59  			v, _ := strconv.ParseFloat(String(any), 32)
    60  			return float32(v)
    61  		}
    62  	}
    63  }
    64  
    65  // Float64 converts `any` to float64.
    66  func Float64(any interface{}) float64 {
    67  	if any == nil {
    68  		return 0
    69  	}
    70  	switch value := any.(type) {
    71  	case float32:
    72  		return float64(value)
    73  	case float64:
    74  		return value
    75  	case []byte:
    76  		// TODO: It might panic here for these types.
    77  		return gbinary.DecodeToFloat64(value)
    78  	default:
    79  		rv := reflect.ValueOf(any)
    80  		switch rv.Kind() {
    81  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    82  			return float64(rv.Int())
    83  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    84  			return float64(rv.Uint())
    85  		case reflect.Uintptr:
    86  			return float64(rv.Uint())
    87  		case reflect.Float32, reflect.Float64:
    88  			// Please Note:
    89  			// When the type is float32 or a custom type defined based on float32,
    90  			// switching to float64 may result in a few extra decimal places.
    91  			return rv.Float()
    92  		case reflect.Bool:
    93  			if rv.Bool() {
    94  				return 1
    95  			}
    96  			return 0
    97  		case reflect.String:
    98  			f, _ := strconv.ParseFloat(rv.String(), 64)
    99  			return f
   100  		case reflect.Ptr:
   101  			if rv.IsNil() {
   102  				return 0
   103  			}
   104  			if f, ok := value.(localinterface.IFloat64); ok {
   105  				return f.Float64()
   106  			}
   107  			return Float64(rv.Elem().Interface())
   108  		default:
   109  			if f, ok := value.(localinterface.IFloat64); ok {
   110  				return f.Float64()
   111  			}
   112  			v, _ := strconv.ParseFloat(String(any), 64)
   113  			return v
   114  		}
   115  	}
   116  }