-
Notifications
You must be signed in to change notification settings - Fork 8
/
number.go
83 lines (74 loc) · 1.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package number
import (
"errors"
"math"
"sort"
"strconv"
)
func SliceInt64ToFloat64(src []int64) []float64 {
out := []float64{}
for _, in := range src {
out = append(out, float64(in))
}
return out
}
func IntSliceDedupe(elems []int, sortElems bool) []int {
if len(elems) == 0 {
return elems
}
deduped := []int{}
mapInts := map[int]int{}
for _, el := range elems {
if _, ok := mapInts[el]; !ok {
deduped = append(deduped, el)
mapInts[el] = 1
}
}
if sortElems {
sort.Ints(deduped)
}
return deduped
}
func IntLength(num int) uint {
if num < 0 {
num = num * -1
}
return uint(len(strconv.Itoa(num)))
}
var ErrOverflow = errors.New("integer overflow")
func Int32(i int) (int32, error) {
if i > math.MaxInt32 || i < math.MinInt32 {
return 0, ErrOverflow
}
return int32(i), nil
}
func Int16(i int) (int16, error) {
if i > math.MaxInt16 || i < math.MinInt16 {
return 0, ErrOverflow
}
return int16(i), nil
}
func Int8(i int) (int8, error) {
if i > math.MaxInt8 || i < math.MinInt8 {
return 0, ErrOverflow
}
return int8(i), nil
}
/*
// fmtInt formats v into the tail of buf.
// It returns the index where the output begins.
func fmtInt(buf []byte, v uint64) int {
w := len(buf)
if v == 0 {
w--
buf[w] = '0'
} else {
for v > 0 {
w--
buf[w] = byte(v%10) + '0'
v /= 10
}
}
return w
}
*/