-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
138 lines (132 loc) · 2.83 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (C) 2022 Satoshi Konno All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package safecast
import (
"fmt"
"strconv"
)
// FromFloat64 casts an interface to an float64 type.
func FromFloat64(from float64, to any) error {
switch to := to.(type) {
case *int:
*to = int(from)
case *int8:
*to = int8(from)
case *int16:
*to = int16(from)
case *int32:
*to = int32(from)
case *int64:
*to = int64(from)
case *uint:
*to = uint(from)
case *uint8:
*to = uint8(from)
case *uint16:
*to = uint16(from)
case *uint32:
*to = uint32(from)
case *uint64:
*to = uint64(from)
case *float32:
*to = float32(from)
case *float64:
*to = from
case *string:
*to = fmt.Sprintf("%v", from)
default:
return newErrorCast(from, to)
}
return nil
}
// FromFloat32 casts an interface to an float32 type.
func FromFloat32(from float32, to any) error {
return FromFloat64(float64(from), to)
}
// ToFloat64 casts an interface to an float64 type.
func ToFloat64(from any, to *float64) error {
switch from := from.(type) {
case int:
*to = float64(from)
case int8:
*to = float64(from)
case int16:
*to = float64(from)
case int32:
*to = float64(from)
case int64:
*to = float64(from)
case uint:
*to = float64(from)
case uint8:
*to = float64(from)
case uint16:
*to = float64(from)
case uint32:
*to = float64(from)
case uint64:
*to = float64(from)
case float32:
*to = float64(from)
case float64:
*to = from
case string:
f, err := strconv.ParseFloat(from, 64)
if err != nil {
return newErrorCast(from, to)
}
*to = f
default:
return newErrorCast(from, to)
}
return nil
}
// ToFloat32 casts an interface to an float64 type.
func ToFloat32(from any, to *float32) error {
switch from := from.(type) {
case int:
*to = float32(from)
case int8:
*to = float32(from)
case int16:
*to = float32(from)
case int32:
*to = float32(from)
case int64:
*to = float32(from)
case uint:
*to = float32(from)
case uint8:
*to = float32(from)
case uint16:
*to = float32(from)
case uint32:
*to = float32(from)
case uint64:
*to = float32(from)
case float32:
*to = from
case float64:
*to = float32(from)
case string:
f, err := strconv.ParseFloat(from, 32)
if err != nil {
return newErrorCast(from, to)
}
*to = float32(f)
default:
return newErrorCast(from, to)
}
return nil
}