-
Notifications
You must be signed in to change notification settings - Fork 0
/
273.go
117 lines (106 loc) · 2.4 KB
/
273.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
package p273
import "bytes"
/**
Convert a non-negative integer to its english words representation.
Given input is guaranteed to be less than 2^31 - 1.
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
*/
func digitToWords(digit int) string {
word := ""
if digit >= 0 && digit < 10 {
word = []string{"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine"}[digit]
}
return word
}
func tenToWords(digit int) string {
word := ""
if digit > 0 && digit < 10 {
word = []string{"Zero", "Ten", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}[digit]
}
return word
}
// convert [10..19] to string
func doubleDigitToWords(digit int) string {
word := ""
if digit >= 10 && digit < 20 {
word = []string{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}[digit-10]
}
return word
}
func unitNumberToWords(num int) string {
buf := bytes.Buffer{}
if num >= 100 {
buf.WriteString(digitToWords(num / 100))
buf.WriteByte(' ')
buf.WriteString("Hundred")
}
num %= 100
if num > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
if num < 10 {
buf.WriteString(digitToWords(num))
num = 0
} else if num < 20 {
buf.WriteString(doubleDigitToWords(num))
num = 0
} else {
buf.WriteString(tenToWords(num / 10))
}
}
num %= 10
if num > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
buf.WriteString(digitToWords(num))
}
return buf.String()
}
func numberToWords(num int) string {
unit := num % 1000
num /= 1000
thousand := num % 1000
num /= 1000
million := num % 1000
num /= 1000
billion := num % 1000
buf := bytes.Buffer{}
if billion > 0 {
buf.WriteString(unitNumberToWords(billion))
buf.WriteByte(' ')
buf.WriteString("Billion")
}
if million > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
buf.WriteString(unitNumberToWords(million))
buf.WriteByte(' ')
buf.WriteString("Million")
}
if thousand > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
buf.WriteString(unitNumberToWords(thousand))
buf.WriteByte(' ')
buf.WriteString("Thousand")
}
if unit > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
buf.WriteString(unitNumberToWords(unit))
}
if buf.Len() == 0 {
buf.WriteString(digitToWords(num))
}
return buf.String()
}