-
Notifications
You must be signed in to change notification settings - Fork 220
/
Convert_numbers_to_word.py
121 lines (107 loc) · 3.61 KB
/
Convert_numbers_to_word.py
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
one_digit_words = {
"0": ["zero"],
"1": ["one"],
"2": ["two", "twen"],
"3": ["three", "thir"],
"4": ["four", "for"],
"5": ["five", "fif"],
"6": ["six"],
"7": ["seven"],
"8": ["eight"],
"9": ["nine"],
}
two_digit_words = ["ten", "eleven", "twelve"]
hundred = "hundred"
large_sum_words = [
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
"sextillion",
"septillion",
"octillion",
"nonillion",
]
def converter(n):
word = []
if n.startswith("-"):
word.append("(negative)")
n = n[1:]
if len(n) % 3 != 0 and len(n) > 3:
n = n.zfill(3 * (((len(n) - 1) // 3) + 1))
sum_list = [n[i : i + 3] for i in range(0, len(n), 3)]
skip = False
for i, num in enumerate(sum_list):
if num != "000":
skip = False
for _ in range(len(num)):
num = num.lstrip("0")
if len(num) == 1:
if (
(
len(sum_list) > 1
or (len(sum_list) == 1 and len(sum_list[0]) == 3)
)
and i == len(sum_list) - 1
and (word[-1] in large_sum_words or hundred in word[-1])
):
word.append("and")
word.append(one_digit_words[num][0])
num = num[1:]
break
if len(num) == 2:
if num[0] != "0":
if (
len(sum_list) > 1
or (len(sum_list) == 1 and len(sum_list[0]) == 3)
) and i == len(sum_list) - 1:
word.append("and")
if num.startswith("1"):
if int(num[1]) in range(3):
word.append(two_digit_words[int(num[1])])
else:
number = one_digit_words[num[1]][
1 if int(num[1]) in range(3, 6, 2) else 0
]
word.append(
number + ("teen" if not number[-1] == "t" else "een")
)
else:
word.append(
one_digit_words[num[0]][
1 if int(num[0]) in range(2, 6) else 0
]
+ ("ty " if num[0] != "8" else "y ")
+ (one_digit_words[num[1]][0] if num[1] != "0" else "")
)
break
else:
num = num[1:]
continue
if len(num) == 3:
if num[0] != "0":
word.append(one_digit_words[num[0]][0] + " " + hundred)
if num[1:] == "00":
break
num = num[1:]
if len(sum_list[i:]) > 1 and not skip:
word.append(large_sum_words[len(sum_list[i:]) - 2])
skip = True
word = " ".join(map(str.strip, word))
return (
word[0].lstrip().upper() + word[1:].rstrip().lower()
if "negative" not in word
else word[:11].lstrip() + word[11].upper() + word[12:].rstrip().lower()
)
if __name__ == "__main__":
while True:
try:
n = input("Enter any number to convert it into words or 'exit' to stop: ")
if n == "exit":
break
int(n)
print(n, "-->", converter(n))
except ValueError:
print("Error: Invalid Number!")