-
Notifications
You must be signed in to change notification settings - Fork 6
/
itoa.go
63 lines (55 loc) · 1.63 KB
/
itoa.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
package gox
import (
"math"
"strings"
"github.com/goexl/gox/internal/callback/constraint"
"github.com/goexl/gox/internal/constant"
"github.com/goexl/gox/internal/number"
"github.com/goexl/gox/internal/number/callback"
)
// FormatInt 增强版进制转换,最大支持76进制
func FormatInt[T constraint.Int](num T, base uint8, callback callback.FormatInt[T]) string {
final := new(strings.Builder)
for 0 != num {
var current string
remainder := int(int64(num) % int64(base))
if constant.MaxFormat > remainder && constant.MaxDecimal < remainder {
current = number.TenToAny[remainder]
} else {
current = callback(T(remainder), 10)
}
final.WriteString(current)
num = num / T(base)
}
return final.String()
}
func FormatUint[T constraint.Uint](num T, base uint8, callback callback.FormatUint[T]) string {
final := new(strings.Builder)
for 0 != num {
var current string
remainder := int(uint64(num) % uint64(base))
if constant.MaxFormat > remainder && constant.MaxDecimal < remainder {
current = number.TenToAny[remainder]
} else {
current = callback(T(remainder), 10)
}
final.WriteString(current)
num = num / T(base)
}
return final.String()
}
// Atoi 增强版进制转换,支持任意进制,最大76进制
func Atoi[T constraint.Int | constraint.Uint](num string, base int) T {
final := 0.0
length := len(strings.Split(num, constant.StringEmpty)) - 1
for _, value := range strings.Split(num, constant.StringEmpty) {
tmp := float64(number.FindKey(value))
if -1 != tmp {
final = final + tmp*math.Pow(float64(base), float64(length))
length = length - 1
} else {
break
}
}
return T(final)
}