utils.go
2.4 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
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
}
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
}
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 + "阶段"
}