utils.go
4.6 KB
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
package utils
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/shopspring/decimal"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/log"
"strconv"
"strings"
)
// Intersect 返回两个数组的交集
func Intersect(nums1 []int64, nums2 []int64) []int64 {
if len(nums1) > len(nums2) {
return Intersect(nums2, nums1)
}
m := map[int64]int64{}
for _, num := range nums1 {
m[num]++
}
var intersection []int64
for _, num := range nums2 {
if m[num] > 0 {
intersection = append(intersection, num)
m[num]--
}
}
return intersection
}
// Intersect32 返回两个数组的交集
func Intersect32(nums1 []int32, nums2 []int32) []int32 {
if len(nums1) > len(nums2) {
return Intersect32(nums2, nums1)
}
m := map[int32]int32{}
for _, num := range nums1 {
m[num]++
}
var intersection []int32
for _, num := range nums2 {
if m[num] > 0 {
intersection = append(intersection, num)
m[num]--
}
}
return intersection
}
// Difference 求差集 slice1-并集
func Difference(slice1, slice2 []int64) []int64 {
m := make(map[int64]int)
nn := make([]int64, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v]++
}
for _, value := range slice1 {
times, _ := m[value]
if times == 0 {
nn = append(nn, value)
}
}
return nn
}
// SliceAtoi 字符创数组转数字数组
func SliceAtoi(sa []string) ([]int64, error) {
si := make([]int64, 0, len(sa))
for _, a := range sa {
i, err := strconv.ParseInt(a, 10, 64)
if err != nil {
return si, err
}
si = append(si, i)
}
return si, nil
}
// SliceItoa 数字数组转字符创数组
func SliceItoa(sa []int64) []string {
si := make([]string, 0, len(sa))
for _, a := range sa {
s := strconv.Itoa(int(a))
si = append(si, s)
}
return si
}
func Round(value float64, places int32) float64 {
quantity := decimal.NewFromFloat(value)
d := quantity.Round(places)
rsp, _ := d.Float64()
return rsp
}
// NumberToCNNumber 数字转中文数字
func NumberToCNNumber(num int) string {
chineseMap := []string{"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"}
chineseNum := []string{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
var listNum []int
for ; num > 0; num = num / 10 {
listNum = append(listNum, num%10)
}
n := len(listNum)
chinese := ""
//注意这里是倒序的
for i := n - 1; i >= 0; i-- {
chinese = fmt.Sprintf("%s%s%s", chinese, chineseNum[listNum[i]], chineseMap[i])
}
//注意替换顺序
for {
copyChinese := chinese
copyChinese = strings.Replace(copyChinese, "零万", "万", 1)
copyChinese = strings.Replace(copyChinese, "零亿", "亿", 1)
copyChinese = strings.Replace(copyChinese, "零十", "零", 1)
copyChinese = strings.Replace(copyChinese, "零百", "零", 1)
copyChinese = strings.Replace(copyChinese, "零千", "零", 1)
copyChinese = strings.Replace(copyChinese, "零零", "", 1)
//copyChinese = strings.Replace(copyChinese, "零圆", "", 1)
//copyChinese = strings.Replace(copyChinese, "零", "", 1)
if copyChinese == chinese {
break
} else {
chinese = copyChinese
}
}
return "第" + chinese + "阶段"
}
// RemoveDuplication 数组去重
func RemoveDuplication(arr []string) []string {
set := make(map[string]struct{}, len(arr))
j := 0
for _, v := range arr {
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
func RemoveDuplicationInt64(arr []int64) []int64 {
set := make(map[int64]struct{}, len(arr))
j := 0
for _, v := range arr {
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
func RemoveDuplicationString(arr []string) []string {
set := make(map[string]struct{}, len(arr))
j := 0
for _, v := range arr {
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
func RemoveDuplicationInt32(arr []int32) []int32 {
set := make(map[int32]struct{}, len(arr))
j := 0
for _, v := range arr {
_, ok := set[v]
if ok {
continue
}
set[v] = struct{}{}
arr[j] = v
j++
}
return arr[:j]
}
// IsContain 判断int32数组是否包含
func IsContain(items []int32, item int32) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
// IsContain64 判断int64数组是否包含
func IsContain64(items []int64, item int64) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
func AnyToHex(any interface{}) []byte {
buff := new(bytes.Buffer)
//数据写入buff
err := binary.Write(buff, binary.BigEndian, any)
if err != nil {
log.Logger.Error(err.Error())
}
return buff.Bytes()
}