-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
123 lines (104 loc) · 2 KB
/
number.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
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
package korean
type NumberOption struct {
// 앞에 "일"을 붙여 읽습니다.
// 가령, 1000을 "일천"으로,
// 10000을 "일만"으로 반환합니다.
MarkNumberOne bool
}
func Number(num int, opt ...NumberOption) []string {
if num == 0 {
return []string{"영"}
}
var option NumberOption
if len(opt) > 0 {
option = opt[0]
} else {
option = NumberOption{false}
}
result := make([]string, 0)
slicedNum := make([]int, 0)
n := num
for n > 0 {
slicedNum = append(slicedNum, n%10000)
n /= 10000
}
for i, v := range slicedNum {
_thousand := v / 1000
_hundred := (v - (_thousand * 1000)) / 100
_ten := (v - (_thousand * 1000) - (_hundred * 100)) / 10
_one := v - (_thousand * 1000) - (_hundred * 100) - (_ten * 10)
var (
thousand, hundred, ten, one, digits string
)
if r := getKoreanNumber(_thousand); r != "" {
if !option.MarkNumberOne && r == "일" {
r = ""
}
thousand = r + "천"
}
if r := getKoreanNumber(_hundred); r != "" {
if !option.MarkNumberOne && r == "일" && _thousand <= 0 {
r = ""
}
hundred = r + "백"
}
if r := getKoreanNumber(_ten); r != "" {
if r == "일" {
r = ""
}
ten = r + "십"
}
if r := getKoreanNumber(_one); r != "" {
one = r
}
digits = getKoreanNumberDigits(i)
if thousand == "" && hundred == "" && ten == "" && one == "" {
digits = ""
}
result = append(
result,
thousand+hundred+ten+one+digits,
)
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return result
}
func getKoreanNumberDigits(i int) string {
switch i {
case 1:
return "만"
case 2:
return "억"
case 3:
return "조"
case 4:
return "경"
default:
return ""
}
}
func getKoreanNumber(n int) string {
switch n {
case 1:
return "일"
case 2:
return "이"
case 3:
return "삼"
case 4:
return "사"
case 5:
return "오"
case 6:
return "육"
case 7:
return "칠"
case 8:
return "팔"
case 9:
return "구"
}
return ""
}