Skip to content

Commit c3c6932

Browse files
committed
feat(core): add agnumber
1 parent fa7268b commit c3c6932

File tree

9 files changed

+578
-6
lines changed

9 files changed

+578
-6
lines changed

agnumber/number.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package agnumber
2+
3+
import (
4+
"math"
5+
"sort"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/pkg/errors"
10+
"github.com/thoas/go-funk"
11+
)
12+
13+
// Float64toa converts float64 value to 10-based string.
14+
// Function takes optional argument - precision - which is described in strconv.FormatFloat
15+
func Float64toa(x float64, precision ...int) string {
16+
p := -1
17+
if len(precision) > 0 {
18+
p = precision[0]
19+
}
20+
s := strconv.FormatFloat(x, 'f', p, 64)
21+
if strings.Count(s, ".") == 1 {
22+
return strings.TrimRight(strings.TrimRight(s, "0"), ".")
23+
}
24+
return s
25+
}
26+
27+
// Atoi64 converts 10-based string into int64 value.
28+
func Atoi64(s string) (int64, error) {
29+
s = strings.TrimSpace(s)
30+
n, err := strconv.ParseInt(s, 10, 64)
31+
return n, errors.Wrap(err, "can't parse int")
32+
}
33+
34+
// Atof64 converts 10-based string into float64 value.
35+
func Atof64(s string) (float64, error) {
36+
s = strings.TrimSpace(s)
37+
f, err := strconv.ParseFloat(s, 64)
38+
return f, errors.Wrap(err, "can't parse float")
39+
}
40+
41+
// MustAtoi64 is similar to Atoi64.
42+
// But it panics if the input can't be parse as an int64
43+
func MustAtoi64(s string) int64 {
44+
i, err := Atoi64(s)
45+
if err != nil {
46+
panic(err)
47+
}
48+
return i
49+
}
50+
51+
// MustAtof64 is similar to Atof64.
52+
// But it panics if the input can't be parse as an float64
53+
func MustAtof64(s string) float64 {
54+
f, err := Atof64(s)
55+
if err != nil {
56+
panic(err)
57+
}
58+
return f
59+
}
60+
61+
// Atoi is similar to strconv.Atoi
62+
// but trims spaces and adds stack to errors
63+
func Atoi(s string) (int, error) {
64+
s = strings.TrimSpace(s)
65+
n, err := strconv.Atoi(s)
66+
return n, errors.Wrapf(err, "can't convert %q to int", s)
67+
}
68+
69+
// MustAtoi converts string to integer and panics otherwise.
70+
func MustAtoi(s string) int {
71+
i, err := Atoi(s)
72+
if err != nil {
73+
panic(err)
74+
}
75+
return i
76+
}
77+
78+
// Min finds the minimum value in int slice
79+
func Min(values ...int) int {
80+
if len(values) == 0 {
81+
return math.MinInt64
82+
}
83+
return int(funk.Reduce(values, math.Min, math.MaxInt64).(float64))
84+
}
85+
86+
// Max finds the maximum value in int slice
87+
func Max(values ...int) int {
88+
if len(values) == 0 {
89+
return math.MaxInt64
90+
}
91+
return int(funk.Reduce(values, math.Max, math.MinInt64).(float64))
92+
}
93+
94+
// MinFloat64 finds the maximum value in float64 slice
95+
func MinFloat64(vals ...float64) float64 {
96+
if len(vals) == 0 {
97+
return -math.MaxFloat64
98+
}
99+
result := vals[0]
100+
for _, v := range vals {
101+
result = math.Min(result, v)
102+
}
103+
return result
104+
}
105+
106+
// MaxFloat64 finds the maximum value in float64 slice
107+
func MaxFloat64(vals ...float64) float64 {
108+
if len(vals) == 0 {
109+
return math.MaxFloat64
110+
}
111+
result := vals[0]
112+
for _, v := range vals {
113+
result = math.Max(result, v)
114+
}
115+
return result
116+
}
117+
118+
// DownFrom generates decreasing numbers from start to end
119+
func DownFrom(start, end uint) []int {
120+
s, e := int(start), int(end)
121+
diff := s - e
122+
if diff < 0 {
123+
return nil
124+
}
125+
nums := make([]int, diff)
126+
for i := range nums {
127+
nums[i] = s - i
128+
}
129+
return nums
130+
}
131+
132+
// UpTo generates increasing numbers from start to end
133+
func UpTo(start, end uint) []int {
134+
s, e := int(start), int(end)
135+
diff := e - s
136+
if diff < 0 {
137+
return nil
138+
}
139+
nums := make([]int, diff)
140+
for i := range nums {
141+
nums[i] = s + i
142+
}
143+
return nums
144+
}
145+
146+
// SortInt64s returns a sorted slice of int64
147+
// follows the same signature as the sort functions in the sort
148+
// standard library sort package
149+
func SortInt64s(s []int64) {
150+
sort.SliceStable(s, func(i, j int) bool { return s[i] < s[j] })
151+
}
152+
153+
// FloorToMostSignificantDigit rounds down to most significant digit
154+
// e.g. 795 -> 700, 11 -> 10, 99 -> 90, 200 -> 200
155+
func FloorToMostSignificantDigit(f float64) float64 {
156+
n := int(f)
157+
i := 0
158+
for n > 10 {
159+
n /= 10
160+
i++
161+
}
162+
return float64(n) * math.Pow10(i)
163+
}
164+
165+
// AbsInt returns the absolute value of n.
166+
// e.g. 795 -> 795, -11 -> 11
167+
func AbsInt(n int) int {
168+
if n < 0 {
169+
return -n
170+
}
171+
return n
172+
}

0 commit comments

Comments
 (0)