Converting Numbers to Words Python

-- Leave a Comment
Hello there, Here is a python code to convert what ever number you enter into words. First it will take a input. It wont ask with any message. Then it give back the number in words form. Here is no any code to handle unexpected user input. This program works for once to Hundred Billion.
Sample input:-
104382426112
Expected Output:-
One Hundred Four Billion Three Hundred Eighty Two Million Four Hundred Twenty Six Thousand One Hundred Twelve

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
one = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
tenp = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
tenp2 = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']


def once(num):
    word = ''
    word = one[int(num)]
    word = word.strip()
    return word


def ten(num):
    word = ''
    if num[0] == '1':
        word = tenp[int(num[1])]
    else:
        text = once(num[1])
        word = tenp2[int(num[0])]
        word = word + " " + text
    word = word.strip()
    return word


def hundred(num):
    word = ''
    text = ten(num[1:])
    word = one[int(num[0])]
    if num[0] != '0':
        word = word + " Hundred "
    word = word + text
    word = word.strip()
    return word


def thousand(num):
    word = ''
    pref = ''
    text = ''
    length = len(num)
    if length == 6:
        text = hundred(num[3:])
        pref = hundred(num[:3])
    if length == 5:
        text = hundred(num[2:])
        pref = ten(num[:2])
    if length == 4:
        text = hundred(num[1:])
        word = one[int(num[0])]
    if num[0] != '0' or num[1] != '0' or num[2] != '0':
        word = word + " Thousand "
    word = word + text
    if length == 6 or length == 5:
        word = pref + word
    word = word.strip()
    return word


def million(num):
    word = ''
    pref = ''
    text = ''
    length = len(num)
    if length == 9:
        text = thousand(num[3:])
        pref = hundred(num[:3])
    if length == 8:
        text = thousand(num[2:])
        pref = ten(num[:2])
    if length == 7:
        text = thousand(num[1:])
        word = one[int(num[0])]
    if num[0] != '0' or num[1] != '0' or num[2] != '0' :
        word = word + " Million "
    word = word + text
    if length == 9 or length == 8:
        word = pref + word
    word = word.strip()
    return word


def billion(num):
    word = ''
    pref = ''
    text = ''
    length = len(num)
    if length == 12:
        text = million(num[3:])
        pref = hundred(num[:3])
    if length == 11:
        text = million(num[2:])
        pref = ten(num[:2])
    if length == 10:
        text = million(num[1:])
        word = one[int(num[0])]
    if num[0] != '0':
        word = word + " Billion "
    word = word + text
    if length == 12 or length == 11:
        word = pref + word
    word = word.strip()
    return word


# 104382426112 One Hundred Four Billion Three Hundred Eighty Two Million Four Hundred Twenty Six Thousand One HUndred Twelve

test = int(input())
a = str(test)
leng = len(a)
if leng == 1:
    if a == '0':
        num = 'Zero'
    else:
        num = once(a)
if leng == 2:
    num = ten(a)
if leng == 3:
    num = hundred(a)
if leng > 3 and leng < 7:
    num = thousand(a)
if leng > 6 and leng < 10:
    num = million(a)
if leng > 9 and leng < 13:
    num = billion(a)
print(num)

If you found it useful or if you are in need of it you can use it for any purpose. You can it extend to use for Trillion and so on.....

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment