utils.go
3.1 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
package utils
import (
"fmt"
"github.com/shopspring/decimal"
"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
}
// 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]
}
// IsContain 判断int32数组是否包含
func IsContain(items []int32, item int32) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}