How to convert a integer value to word in python

To convert a integer value to word in python our aim is to convert 124 to “One Hundred Twenty Four”

Here is python code to convert integer value to word

def Towords(num):
	under_20 = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
	tens = ['Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
	above_100 = {100: 'Hundred',1000:'Thousand', 1000000:'Million', 1000000000:'Billion'}
	if num < 20:
		 return under_20[num]

	if num < 100:
		return tens[(int)(num/10)-2] + ('' if num%10==0 else ' ' + under_20[num%10])
	# find the appropriate pivot - 'Million' in 3,603,550, or 'Thousand' in 603,550
	pivot = max([key for key in above_100.keys() if key <= num])
	return Towords((int)(num/pivot)) + ' ' + above_100[pivot] + ('' if num%pivot==0 else ' ' + Towords(num%pivot))


print(Towords(124))

Output

One Hundred Twenty Four

How to convert a integer value to word in python

Leave a Reply

Your email address will not be published. Required fields are marked *