-
Notifications
You must be signed in to change notification settings - Fork 0
/
gconv_float.go
55 lines (50 loc) · 1.12 KB
/
gconv_float.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv
import (
"strconv"
"github.com/gogf/gf/v2/encoding/gbinary"
)
// Float32 converts `any` to float32.
func Float32(any interface{}) float32 {
if any == nil {
return 0
}
switch value := any.(type) {
case float32:
return value
case float64:
return float32(value)
case []byte:
return gbinary.DecodeToFloat32(value)
default:
if f, ok := value.(iFloat32); ok {
return f.Float32()
}
v, _ := strconv.ParseFloat(String(any), 64)
return float32(v)
}
}
// Float64 converts `any` to float64.
func Float64(any interface{}) float64 {
if any == nil {
return 0
}
switch value := any.(type) {
case float32:
return float64(value)
case float64:
return value
case []byte:
return gbinary.DecodeToFloat64(value)
default:
if f, ok := value.(iFloat64); ok {
return f.Float64()
}
v, _ := strconv.ParseFloat(String(any), 64)
return v
}
}