/
helper-num.go
236 lines (191 loc) · 5.2 KB
/
helper-num.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package helper
import (
"math"
"strconv"
"strings"
"time"
)
/*
* Copyright 2020-2023 Aldelo, LP
*
* 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.
*/
// AbsInt returns absolute value of i
func AbsInt(i int) int {
if i < 0 {
return i * -1
} else {
return i
}
}
// AbsInt64 returns absolute value of i
func AbsInt64(i int64) int64 {
if i < 0 {
return i * -1
} else {
return i
}
}
// AbsDuration returns absolute value of d
func AbsDuration(d time.Duration) time.Duration {
if d < 0 {
return d * -1
} else {
return d
}
}
// AbsFloat64 returns absolute value of f
func AbsFloat64(f float64) float64 {
if f < 0 {
return f * -1
} else {
return f
}
}
// IsInt32 tests if input string is integer (whole numbers 32 bits)
func IsInt32(s string) bool {
if _, err := strconv.ParseInt(strings.TrimSpace(s), 10, 32); err != nil {
return false
}
return true
}
// IsInt64 tests if input string is big integer (whole number greater 64 bits)
func IsInt64(s string) bool {
if _, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64); err != nil {
return false
}
return true
}
// IsFloat32 tests if input string is float 32 bit (decimal point value)
func IsFloat32(s string) bool {
if _, err := strconv.ParseFloat(strings.TrimSpace(s), 32); err != nil {
return false
}
return true
}
// IsFloat64 tests if input string is float 64 bit (decimal point value)
func IsFloat64(s string) bool {
if _, err := strconv.ParseFloat(strings.TrimSpace(s), 64); err != nil {
return false
}
return true
}
// IsBoolType tests if input string is boolean
func IsBoolType(s string) bool {
x := []string{"yes", "on", "running", "started"}
if StringSliceContains(&x, strings.ToLower(s)) {
s = "true"
}
if _, err := strconv.ParseBool(s); err != nil {
return false
}
return true
}
// ParseInt32 tests and parses if input string is integer (whole numbers 32 bits)
func ParseInt32(s string) (int, bool) {
if strings.Index(s, ".") >= 0 {
s = SplitString(s, ".", 0)
}
var result int64
var err error
if result, err = strconv.ParseInt(strings.TrimSpace(s), 10, 32); err != nil {
return 0, false
}
if result >= math.MinInt && result <= math.MaxInt {
return int(result), true
} else {
return 0, false
}
}
// ParseInt64 tests and parses if input string is big integer (whole number greater 64 bits)
func ParseInt64(s string) (int64, bool) {
if strings.Index(s, ".") >= 0 {
s = SplitString(s, ".", 0)
}
var result int64
var err error
if result, err = strconv.ParseInt(strings.TrimSpace(s), 10, 64); err != nil {
return 0, false
}
return result, true
}
// ParseFloat32 tests and parses if input string is float 32 bit (decimal point value)
func ParseFloat32(s string) (float32, bool) {
var result float64
var err error
if result, err = strconv.ParseFloat(strings.TrimSpace(s), 32); err != nil {
return 0.00, false
}
return float32(result), true
}
// ParseFloat64 tests and parses if input string is float 64 bit (decimal point value)
func ParseFloat64(s string) (float64, bool) {
var result float64
var err error
if result, err = strconv.ParseFloat(strings.TrimSpace(s), 64); err != nil {
return 0.00, false
}
return result, true
}
// ParseBool tests and parses if input string is boolean,
// return value 1st bool is the boolean result,
// return value 2nd bool is the ParseBool success or failure indicator
func ParseBool(s string) (bool, bool) {
var result bool
var err error
x := []string{"yes", "on", "running", "started", "y", "1"}
if StringSliceContains(&x, strings.ToLower(s)) {
s = "true"
}
if result, err = strconv.ParseBool(s); err != nil {
return false, false
}
return result, true
}
// ExponentialToNumber converts exponential representation of a number into actual number equivalent
func ExponentialToNumber(exp string) string {
if strings.Index(strings.ToLower(exp), "e") >= 0 {
v, _ := ParseFloat64(exp)
return strconv.FormatFloat(v, 'f', -1, 64)
} else {
return exp
}
}
// RoundFloat64 converts float64 value to target precision
func RoundFloat64(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}
// RoundIntegerToNearestBlock returns conformed block size value where val fall into.
// For example: if val is 3 and blockSize is 5, then return value is 5;
// where as if val is 13 and blockSize is 5, then return value is 15;
// val must be > 0
func RoundIntegerToNearestBlock(val int, blockSize int) int {
if val < 0 {
return -1
}
if blockSize < 1 {
return -1
}
if val == 0 {
return blockSize
}
number := float64(val) / float64(blockSize)
whole := RoundFloat64(number, 0)
frac := number - whole
if frac <= 0 {
return int(whole) * blockSize
} else {
return (int(whole) + 1) * blockSize
}
}