Number to Words Conversion in Python Without Any Library
This python program converts given integer number to its equivalent words without using any external library.
This number to word conversion python program supports up to 12 digits number. For large number, we encourage you to modify this program :)
Python Source Code: Number to Words Conversion
# Number to Words
# Main Logic
ones = ('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine')
twos = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen')
tens = ('Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety', 'Hundred')
suffixes = ('', 'Thousand', 'Million', 'Billion')
def process(number, index):
if number=='0':
return 'Zero'
length = len(number)
if(length > 3):
return False
number = number.zfill(3)
words = ''
hdigit = int(number[0])
tdigit = int(number[1])
odigit = int(number[2])
words += '' if number[0] == '0' else ones[hdigit]
words += ' Hundred ' if not words == '' else ''
if(tdigit > 1):
words += tens[tdigit - 2]
words += ' '
words += ones[odigit]
elif(tdigit == 1):
words += twos[(int(tdigit + odigit) % 10) - 1]
elif(tdigit == 0):
words += ones[odigit]
if(words.endswith('Zero')):
words = words[:-len('Zero')]
else:
words += ' '
if(not len(words) == 0):
words += suffixes[index]
return words;
def getWords(number):
length = len(str(number))
if length>12:
return 'This program supports upto 12 digit numbers.'
count = length // 3 if length % 3 == 0 else length // 3 + 1
copy = count
words = []
for i in range(length - 1, -1, -3):
words.append(process(str(number)[0 if i - 2 < 0 else i - 2 : i + 1], copy - count))
count -= 1;
final_words = ''
for s in reversed(words):
temp = s + ' '
final_words += temp
return final_words
# End Main Logic
# Reading number from user
number = int(input('Enter any number: '))
print('%d in words is: %s' %(number, getWords(number)))
Output
Run 1: ---------- Enter any number: 0 0 in words is: Zero Run 2: ---------- Enter any number: 17 17 in words is: Seventeen Run 3: ---------- Enter any number: 100 100 in words is: One Hundred Run 4: ---------- Enter any number: 100001 100001 in words is: One Hundred Thousand One Run 5: ---------- Enter any number: 1234567890 1234567890 in words is: One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety
For 'and' Requirement
In case if you need to append 'and' if number is over one hundred and either of the last two digits are not a zero then you can modify above program as:
# Number to Words
# Main Logic
ones = ('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine')
twos = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen')
tens = ('Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety', 'Hundred')
suffixes = ('', 'Thousand', 'Million', 'Billion')
def process(number, index, ln):
print(">>", ln)
if number=='0':
return 'Zero'
length = len(number)
if(length > 3):
return False
number = number.zfill(3)
words = ''
hdigit = int(number[0])
tdigit = int(number[1])
odigit = int(number[2])
words += '' if number[0] == '0' else ones[hdigit]
words += ' Hundred ' if not words == '' else ''
if index==0 and ln>3:
words+=' and '
elif words=='':
words+=''
elif index==0 and tdigit==0 and odigit==0:
words+=''
elif index==0:
words+= ' and '
else:
words+=''
if(tdigit > 1):
words += tens[tdigit - 2]
words += ' '
words += ones[odigit]
elif(tdigit == 1):
words += twos[(int(tdigit + odigit) % 10) - 1]
elif(tdigit == 0):
words += ones[odigit]
if(words.endswith('Zero')):
words = words[:-len('Zero')]
else:
words += ' '
if(not len(words) == 0):
words += suffixes[index]
return words;
def getWords(number):
length = len(str(number))
if length>12:
return 'This program supports upto 12 digit numbers.'
count = length // 3 if length % 3 == 0 else length // 3 + 1
copy = count
words = []
for i in range(length - 1, -1, -3):
words.append(process(str(number)[0 if i - 2 < 0 else i - 2 : i + 1], copy - count, length))
count -= 1;
final_words = ''
for s in reversed(words):
temp = s + ' '
final_words += temp
return final_words
# End Main Logic
# Reading number from user
number = int(input('Enter any number: '))
print('%d in words is: %s' %(number, getWords(number)))
Output
Run 1: ---------- Enter any number: 0 0 in words is: Zero Run 2: ---------- Enter any number: 17 17 in words is: Seventeen Run 3: ---------- Enter any number: 100 100 in words is: One Hundred Run 4: ---------- Enter any number: 100001 100001 in words is: One Hundred Thousand and One Run 5: ---------- Enter any number: 1234567890 1234567890 in words is: One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred and Ninety