Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 889e25b

Browse files
Niraj shresthaNiraj shrestha
Niraj shrestha
authored and
Niraj shrestha
committed
Finalized
1 parent fd13e61 commit 889e25b

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import math
2+
3+
one_digit_words = {
4+
'0': ["zero"],
5+
'1': ["one"],
6+
'2': ["two", "twen"],
7+
'3': ["three", "thir"],
8+
'4': ["four", "for"],
9+
'5': ["five", "fif"],
10+
'6': ["six"],
11+
'7': ["seven"],
12+
'8': ["eight"],
13+
'9': ["nine"],
14+
}
15+
16+
two_digit_words = ["ten", "eleven", "twelve"]
17+
hundred = "hundred"
18+
large_sum_words = ["thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion"]
19+
20+
def converter(n):
21+
word = []
22+
23+
if n.startswith('-'):
24+
word.append("(negative)")
25+
n = n[1:]
26+
27+
if len(n) % 3 != 0 and len(n) > 3:
28+
n = n.zfill(3 * (((len(n)-1) // 3) + 1))
29+
30+
sum_list = [n[i:i + 3] for i in range(0, len(n), 3)]
31+
skip = False
32+
33+
for i, num in enumerate(sum_list):
34+
if num != '000': skip = False
35+
36+
for _ in range(len(num)):
37+
num = num.lstrip('0')
38+
if len(num) == 1:
39+
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]):
40+
word.append("and")
41+
word.append(one_digit_words[num][0])
42+
num = num[1:]
43+
break
44+
45+
if len(num) == 2:
46+
if num[0] != '0':
47+
if (len(sum_list) > 1 or (len(sum_list) == 1 and len(sum_list[0]) == 3)) and i == len(sum_list) - 1:
48+
word.append("and")
49+
if num.startswith('1'):
50+
if int(num[1]) in range(3):
51+
word.append(two_digit_words[int(num[1])])
52+
else:
53+
number = one_digit_words[num[1]][1 if int(num[1]) in range(3, 6, 2) else 0]
54+
word.append(number + ("teen" if not number[-1] == 't' else "een"))
55+
else:
56+
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 ""))
57+
break
58+
else:
59+
num = num[1:]
60+
continue
61+
62+
if len(num) == 3:
63+
if num[0] != '0':
64+
word.append(one_digit_words[num[0]][0] + " " + hundred)
65+
if num[1:] == '00': break
66+
num = num[1:]
67+
68+
if len(sum_list[i:]) > 1 and not skip:
69+
word.append(large_sum_words[len(sum_list[i:]) - 2])
70+
skip = True
71+
72+
word = " ".join(map(str.strip, word))
73+
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()
74+
75+
if __name__ == "__main__":
76+
while True:
77+
try:
78+
n = input("Enter any number to convert it into words or 'exit' to stop: ")
79+
if n == "exit":
80+
break
81+
int(n)
82+
print(n, "-->", converter(n))
83+
except ValueError as e:
84+
print("Error: Invalid Number!")

0 commit comments

Comments
 (0)