-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_to_words.go
81 lines (75 loc) · 1.75 KB
/
int_to_words.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
package problem0273
import (
"fmt"
"strconv"
"strings"
)
/*
Convert a non-negative integer num to its English words representation.
*/
func numberToWords(num int) string {
var digits string
var sb strings.Builder
if num == 0 {
return "Zero"
}
digits = fmt.Sprintf("%010d", num)
bils, _ := strconv.Atoi(digits[0:1])
mils, _ := strconv.Atoi(digits[1:4])
thus, _ := strconv.Atoi(digits[4:7])
ones, _ := strconv.Atoi(digits[7:10])
if bils > 0 {
sb.WriteString(GetQuantifier(bils))
sb.WriteString("Billion ")
}
if mils > 0 {
sb.WriteString(GetQuantifier(mils))
sb.WriteString("Million ")
}
if thus > 0 {
sb.WriteString(GetQuantifier(thus))
sb.WriteString("Thousand ")
}
if ones > 0 {
sb.WriteString(GetQuantifier(ones))
}
digits = sb.String()
return digits[:len(digits)-1]
}
func GetQuantifier(input int) string {
var sb strings.Builder
if input >= 100 {
sb.WriteString(Words[input/100])
sb.WriteString(" Hundred ")
}
input %= 100
if input <= 0 {
return sb.String()
}
if input > 19 {
sb.WriteString(Words[(input/10)*10])
sb.WriteString(" ")
input %= 10
if input > 0 {
sb.WriteString(Words[input])
sb.WriteString(" ")
}
} else {
sb.WriteString(Words[input])
sb.WriteString(" ")
}
return sb.String()
}
var Words = map[int]string{
1: "One", 2: "Two", 3: "Three",
4: "Four", 5: "Five", 6: "Six",
7: "Seven", 8: "Eight", 9: "Nine",
10: "Ten", 11: "Eleven", 12: "Twelve",
13: "Thirteen", 14: "Fourteen", 15: "Fifteen",
16: "Sixteen", 17: "Seventeen", 18: "Eighteen",
19: "Nineteen", 20: "Twenty", 30: "Thirty",
40: "Forty", 50: "Fifty", 60: "Sixty",
70: "Seventy", 80: "Eighty", 90: "Ninety",
}
// Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven
// 2|147|483|6|47