-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
61 lines (51 loc) · 1.44 KB
/
number.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
package agnumber
import (
"math"
"strconv"
"github.com/pkg/errors"
)
// F64toa converts float64 value to 10-based string.
// Function takes optional argument - precision - which is described in strconv.FormatFloat
func F64toa(x float64, precision ...int) string {
p := -1
if len(precision) > 0 {
p = precision[0]
}
return strconv.FormatFloat(x, 'f', p, 64)
}
// Atoi64 converts 10-based string into int64 value.
func Atoi64(s string) (int64, error) {
n, err := strconv.ParseInt(s, 10, 64)
return n, errors.Wrap(err, "can't parse int")
}
// Atof64 converts 10-based string into float64 value.
func Atof64(s string) (float64, error) {
f, err := strconv.ParseFloat(s, 64)
return f, errors.Wrap(err, "can't parse float")
}
// I64toa converts int64 value to 10-based string
func I64toa(x int64) string {
return strconv.FormatInt(x, 10)
}
// I64tox converts int64 value to 16-based string
func I64tox(x int64) string {
return strconv.FormatInt(x, 16)
}
func reduceIntSlice(a []int, start int, reduce func(float64, float64) float64) int {
if len(a) == 0 {
return start
}
result := a[0]
for _, v := range a {
result = int(reduce(float64(result), float64(v)))
}
return result
}
// Min finds the minimum value in int slice
func Min(values ...int) int {
return reduceIntSlice(values, int(math.MinInt64), math.Min)
}
// Max finds the maximum value in int slice
func Max(values ...int) int {
return reduceIntSlice(values, int(math.MaxInt64), math.Max)
}