-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnum2words.go
206 lines (179 loc) · 5.57 KB
/
num2words.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// ============================================================================
// = num2words.go
// = Description: Converts a number to its English counterpart
// = Allows arbitrary precision numbers
// = Based on: https://github.com/divan/num2words
// = Date: December 12, 2021
// ============================================================================
package num2words
import (
"math/big"
)
var unique = []string{
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
}
var tens_words = []string{
"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety",
}
// 64-bit ints go up to quintillion, big.Int is arbitrarily large
// IMPORTANT: this uses the short-scale system for large numbers
var more = []string{
"hundred", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion", "sextillion", "septillion",
"octillion", "nonillion", "decillion", "undecillion",
"duodecillion", "tredecillion", "quattuordecillion",
"quindecillion", "sexdecillion", "septendecillion",
"octodecillion", "novemdecillion",
"vigintillion", // vigintillion is 10^63
}
// cent is 10^303, need to manage the gap b/w vigintillion & this
var vigin = "vigintillion" // nothing defined b/w this & cent
var cent = "centillion" // nothing defined after this
// 1,234,567,890,123,456,789
/**
* Num2Words converts a word to its English counterpart.
* For instance, 6 converts to six.
* 235 can convert to two hundred thirty five if sep is false
* or two hundred and thirty-five if sep is true.
* @param num number to convert
* @param sep whether to include separators like 'and' or hyphens
* @return string representation of num
*/
func Num2Words(num *big.Int, sep bool) string {
// base cases
if greaterEqual(num, zero()) && lessEqual(num, newint(19)) {
return unique[num.Int64()]
} else if greaterEqual(num, newint(-19)) && lessEqual(num, zero()) {
return "negative " + unique[abs(num).Int64()]
}
// init
ret := "" // string to return
ndigits := countDigits(num) // # of digits
// check for very large numbers
if greater(ndigits, newint(303)) { // num > centillion
// break up the number
divisor := pow(newint(10), newint(303))
left := div(num, divisor) // left most digits
right := mod(num, divisor) // right most digits < 10^303
// if right is zero, then Num2Words(right) is just vigintillion
if equals(right, zero()) {
ret += Num2Words(left, sep) + " " + cent
} else {
ret += Num2Words(left, sep) + " " + cent + addcomma(sep) + Num2Words(right, sep)
}
} else if greater(ndigits, newint(63)) { // num > vigintillion
// break up the number
divisor := pow(newint(10), newint(63))
left := div(num, divisor) // left most digits
right := abs(mod(num, divisor)) // right most digits
// if right is zero, then Num2Words(right) is just vigintillion
if equals(right, zero()) {
ret += Num2Words(left, sep) + " " + vigin
} else {
ret += Num2Words(left, sep) + " " + vigin + addcomma(sep) + Num2Words(right, sep)
}
} else { // num < vigintillion
ret += convertNum(num, sep)
}
return ret
}
// used to convert nums < 10^303 (i.e. centillion or below)
func convertNum(num *big.Int, sep bool) string {
// inits
pos := abs(num)
ndigits := countDigits(num)
ngroups := div(ndigits, newint(3)).Int64() + 1
// separate into 3-digit groups
groups := make([]int64, 0) // 3-digit groups won't need big.Int
for i := int64(0); i < ngroups; i++ {
groups = append(groups, mod(pos, newint(1000)).Int64())
pos.Div(pos, newint(1000))
}
// convert each of the groups
strgroups := make([]string, len(groups))
for i := 0; i < len(groups); i++ {
strgroups[i] = convertGroup(groups[i], sep)
}
// assemble (and handle larger denominations than hundreds)
str := strgroups[0]
for i := int64(1); i < ngroups; i++ {
if groups[i] != 0 {
prefix := ""
// num is centillion or more (although, it's limited anyway)
if i >= 101 {
prefix = strgroups[i] + " " + cent
} else if i >= 22 { // vigintillion < num < centillion
prefix = strgroups[i] + " " + vigin
} else {
prefix = strgroups[i] + " " + more[i]
}
if len(str) != 0 {
prefix += addcomma(sep)
}
str = prefix + str
}
}
// set sign
if num.Sign() == -1 {
str = "negative " + str
}
return str
}
// this does the "hundreds" of whatever denom we're in
// so, if i've got 324, it would return three hundred [and] twenty[-]four
// and this would denote how many thousands or millions or whatever
func convertGroup(group int64, sep bool) string {
// init
hundreds := group / 100
rem := group % 100
out := "" // string to return
// convert hundreds to words
if hundreds != 0 {
out += unique[hundreds] + " " + more[0]
if rem != 0 {
out += addand(sep)
}
}
// compute tens to words
tens := rem / 10 // get how many tens
ones := rem % 10
if tens >= 2 {
out += tens_words[tens]
if ones != 0 {
out += adddash(sep) + unique[ones]
}
} else if rem != 0 {
out += unique[rem]
}
return out
}
func countDigits(num *big.Int) *big.Int {
a := copy(num) // create deep copy of number so we don't modify the original
count := newint(0)
for !equals(a, newint(0)) { // int division guaranteed to hit zero
a = div(a, newint(10))
count = add(count, newint(1))
}
return count
}
func addand(sep bool) string {
if sep {
return " and "
}
return " "
}
func adddash(sep bool) string {
if sep {
return "-"
}
return " "
}
func addcomma(sep bool) string {
if sep {
return ", "
}
return " "
}