-
Notifications
You must be signed in to change notification settings - Fork 0
/
triples.go
183 lines (180 loc) · 3.48 KB
/
triples.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
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
package rusnum
import (
"fmt"
"strings"
)
func tripleInWords(triple int64, gender GrammaticalGender) (string, error) {
if !(0 <= triple && triple <= 999) {
return "", fmt.Errorf("triple out of range: '%d'", triple)
}
if triple == 0 {
return zeroInWords, nil
}
ones := triple % 10 // number of ones
tens := triple / 10 % 10 // number of tens
hundreds := triple / 100 % 10 // number of hundreds
var o string
if ones > 0 {
if tens == 1 {
switch ones {
case 1:
o = "одиннадцать"
case 2:
o = "двенадцать"
case 3:
o = "тринадцать"
case 4:
o = "четырнадцать"
case 5:
o = "пятнадцать"
case 6:
o = "шестнадцать"
case 7:
o = "семнадцать"
case 8:
o = "восемнадцать"
case 9:
o = "девятнадцать"
}
} else { // tens != 1
switch ones {
case 1:
switch gender {
case Neuter:
o = "одно"
case Masculine:
o = "один"
case Feminine:
o = "одна"
}
case 2:
switch gender {
case Feminine:
o = "две"
default:
o = "два"
}
case 3:
o = "три"
case 4:
o = "четыре"
case 5:
o = "пять"
case 6:
o = "шесть"
case 7:
o = "семь"
case 8:
o = "восемь"
case 9:
o = "девять"
}
}
} // if ones > 0
var t string
if (tens > 1) || ((tens == 1) && (ones == 0)) {
switch tens {
case 1:
t = "десять"
case 2:
t = "двадцать"
case 3:
t = "тридцать"
case 4:
t = "сорок"
case 5:
t = "пятьдесят"
case 6:
t = "шестьдесят"
case 7:
t = "семьдесят"
case 8:
t = "восемьдесят"
case 9:
t = "девянoсто"
}
} // if (tens > 1) || ((tens == 1) && (ones == 0))
var h string
if hundreds > 0 {
switch hundreds {
case 1:
h = "сто"
case 2:
h = "двести"
case 3:
h = "триста"
case 4:
h = "четыреста"
case 5:
h = "пятьсот"
case 6:
h = "шестьсот"
case 7:
h = "семьсот"
case 8:
h = "восемьсот"
case 9:
h = "девятьсот"
}
} // if hundreds > 0
var sb strings.Builder
if len(h) > 0 {
sb.WriteString(h)
}
if len(t) > 0 {
if sb.Len() > 0 {
sb.WriteRune(' ')
}
sb.WriteString(t)
}
if len(o) > 0 {
if sb.Len() > 0 {
sb.WriteRune(' ')
}
sb.WriteString(o)
}
return sb.String(), nil
}
// triplesInWords returns triples comprising 'n' in russian words.
func triplesInWords(n int64, withZeros bool, gender GrammaticalGender) []string {
// 'n' must not be negative
if n == 0 {
return []string{zeroInWords}
}
var tt []string
tripleNumber := 0
for n > 0 {
triple := n % 1000
t := ""
if triple > 0 || withZeros {
if tripleNumber == 0 {
t, _ = tripleInWords(triple, gender)
} else {
if tripleNumber == 1 {
t, _ = tripleInWords(triple, Feminine)
} else {
t, _ = tripleInWords(triple, Masculine)
}
nnc := getNumeralNumberCase(triple)
switch tripleNumber {
case 1:
t += " " + thousandNumberCase[nnc]
case 2:
t += " " + millionNumberCase[nnc]
case 3:
t += " " + milliardNumberCase[nnc]
case 4:
t += " " + trillionNumberCase[nnc]
case 5:
t += " " + quadrillionNumberCase[nnc]
case 6:
t += " " + quintillionNumberCase[nnc]
}
}
}
tt = append(tt, t)
tripleNumber++
n /= 1000
}
return tt
}