gitee.com/go-spring2/spring-base@v1.1.3/cast/float.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cast
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  )
    23  
    24  func Float32Ptr(s float32) *float32 { return &s }
    25  func Float64Ptr(s float64) *float64 { return &s }
    26  
    27  // ToFloat32 casts an interface{} to a float32.
    28  // When type is clear, it is recommended to use standard library functions.
    29  func ToFloat32(i interface{}) float32 {
    30  	v, _ := ToFloat64E(i)
    31  	return float32(v)
    32  }
    33  
    34  // ToFloat64 casts an interface{} to a float64.
    35  // When type is clear, it is recommended to use standard library functions.
    36  func ToFloat64(i interface{}) float64 {
    37  	v, _ := ToFloat64E(i)
    38  	return v
    39  }
    40  
    41  // ToFloat64E casts an interface{} to a float64.
    42  // When type is clear, it is recommended to use standard library functions.
    43  func ToFloat64E(i interface{}) (float64, error) {
    44  	switch s := i.(type) {
    45  	case nil:
    46  		return 0, nil
    47  	case int:
    48  		return float64(s), nil
    49  	case int8:
    50  		return float64(s), nil
    51  	case int16:
    52  		return float64(s), nil
    53  	case int32:
    54  		return float64(s), nil
    55  	case int64:
    56  		return float64(s), nil
    57  	case *int:
    58  		return float64(*s), nil
    59  	case *int8:
    60  		return float64(*s), nil
    61  	case *int16:
    62  		return float64(*s), nil
    63  	case *int32:
    64  		return float64(*s), nil
    65  	case *int64:
    66  		return float64(*s), nil
    67  	case uint:
    68  		return float64(s), nil
    69  	case uint8:
    70  		return float64(s), nil
    71  	case uint16:
    72  		return float64(s), nil
    73  	case uint32:
    74  		return float64(s), nil
    75  	case uint64:
    76  		return float64(s), nil
    77  	case *uint:
    78  		return float64(*s), nil
    79  	case *uint8:
    80  		return float64(*s), nil
    81  	case *uint16:
    82  		return float64(*s), nil
    83  	case *uint32:
    84  		return float64(*s), nil
    85  	case *uint64:
    86  		return float64(*s), nil
    87  	case float32:
    88  		return float64(s), nil
    89  	case float64:
    90  		return s, nil
    91  	case *float32:
    92  		return float64(*s), nil
    93  	case *float64:
    94  		return *s, nil
    95  	case string:
    96  		return strconv.ParseFloat(s, 64)
    97  	case *string:
    98  		return strconv.ParseFloat(*s, 64)
    99  	case bool:
   100  		if s {
   101  			return 1, nil
   102  		}
   103  		return 0, nil
   104  	case *bool:
   105  		if *s {
   106  			return 1, nil
   107  		}
   108  		return 0, nil
   109  	default:
   110  		return 0, fmt.Errorf("unable to cast type (%T) to float64", i)
   111  	}
   112  }