Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number to english equivalent

Status
Not open for further replies.

didsbub

Programmer
Nov 5, 2006
1
0
0
CA
I need to make a program the converts a number to its english equivalent (ex. 1234 - one thousand two hundred thrity four) The number has to be between 1 and 9999.99. Any help would be appreciated.
 
Hello disbub,

Here is my quick solution which works up to 9999

Code:
import string

digits ={1:"one",2:"two",3:"three",4:"four",5:"five",
         6:"six",7:"seven",8:"eight",9:"nine"}

tens={1:"ten",2:"twenty",3:"thirty",4:"fourty",5:"fifty",
      6:"sixty",7:"seventy",8:"eighty",9:"ninty"}

special={11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",
         16:"sixteen",17:"seventien",18:"eighteen",19:"nineteen"}

order={1:"ten",2:"hundert",3:"thousand",4:"tentousand",5:"hundret tousand"}

result = []
tens_counter= 0
number=''
while not number.isdigit():
  number = raw_input("Enter a number:")
  
n=int(number)
while n > 0:
  print "n = %d" % n
  # special 11..19
  if tens_counter == 0:
    last_two_digits = n % 100 
    if last_two_digits in special.keys():
      print special[last_two_digits]
      result.append(special[last_two_digits])
      n = n/100
      tens_counter += 2
      continue
  # normal    
  digit = n % 10
  if tens_counter == 0:
     print digits[digit]
     result.append(digits[digit])
  elif tens_counter == 1:
     print tens[digit]
     result.append(tens[digit])
  elif tens_counter >=2:
     print digits[digit]+"-"+ order[tens_counter]
     result.append(digits[digit]+"-"+ order[tens_counter])
  n = n / 10
  tens_counter += 1

#print result
result.reverse()
#print result
print "The Result is: %s" % string.join(result)

If you want improve it, so that it works with ten-tousands you need to do it similar as I done with numbers ending with 11-19.
 
Fix: to translate numbers ending with 0 (zero),
change the if-conditon after digit = n % 10 to
Code:
  if tens_counter == 0 and digit != 0:
     print digits[digit]
     result.append(digits[digit])
 
at last - hopefully this source for numbers to 9999 has no more bugs :)
Code:
import string

digits ={1:"one",2:"two",3:"three",4:"four",5:"five",
         6:"six",7:"seven",8:"eight",9:"nine"}

tens={1:"ten",2:"twenty",3:"thirty",4:"fourty",5:"fifty",
      6:"sixty",7:"seventy",8:"eighty",9:"ninty"}

special={11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",
         16:"sixteen",17:"seventien",18:"eighteen",19:"nineteen"}

order={1:"ten",2:"hundert",3:"thousand",4:"tenthousand",5:"hundret tousand"}

result = []
tens_counter= 0
number=''
while not number.isdigit():
  number = raw_input("Enter a number:")
  
n=int(number)
while n > 0:
  print "tens_counter = %d" % tens_counter	
  print "n = %d" % n
  # special 11..19
  if tens_counter == 0:
    last_two_digits = n % 100 
    if last_two_digits in special.keys():
      print special[last_two_digits]
      result.append(special[last_two_digits])
      n = n/100
      tens_counter += 2
      continue

  # normal    
  digit = n % 10
  print "digit = %d" % digit
  if tens_counter == 0:
     if digit != 0:
       print digits[digit]
       result.append(digits[digit])
  elif tens_counter == 1:
     if digit != 0:	  
       print tens[digit]
       result.append(tens[digit])
  elif tens_counter >=2:
     if digit != 0:
       print digits[digit]+"-"+ order[tens_counter]
       result.append(digits[digit]+"-"+ order[tens_counter])
  n = n / 10
  tens_counter += 1

#print result
result.reverse()
#print result
print "The Result is: %s" % string.join(result)
for number above 9999 it would be a little bit more complicated, we need then store the numbers in the list (or stack) too (not only the words) and do the similar circus with ten- and hundert-thousands/millions.. as we done with tens and hunderts...but this would be other program.
 
Another way to look at the same problem. Should work for any number with 12 digits (not counting decimal) but after that it will have rounding issues during the necessary string conversions:
Code:
from __future__ import division
import string

words={	1:"One ", 2:"Two ", 3:"Three ", 4: "Four", 5:"Five ", 6:"Six ", 7:"Seven ", 8:"Eight ", 
		9:"Nine ", 10:"Ten ", 11:"Eleven ", 12:"Twelve ", 13:"Thirteen ", 14:"Fourteen ", 
		15:"Fifteen ", 16:"Sixteen ", 17:"Seventeen ",18: "Eighteen ", 19:"Nineteen ", 
		20:"Twenty ", 30:"Thirty ", 40: "Forty ", 50:"Fifty ", 60:"Sixty ", 70:"Seventy ", 
		80: "Eighty ", 90:"Ninety ", 100:"Hundred ", 1000:"Thousand ", 1000000:"Million ",
		1000000000:"Billion " }

def cword(num,decAsLongWord=False,allowZero=False):
	ww = ''
	if num == 0:
		return (allowZero and "Zero " or "")
	
	ltp = num
	keys = words.keys()
	keys.sort(reverse=True)
	
	for key in keys:
		if ltp >= key:
			if key >= 100:
				ww += cword(ltp//key) + words[key]
				ltp = ltp - (ltp//key * key)
			else:
				ww += words[key]
				ltp = ltp - key
				
		if ltp < 1 and ltp > 0:	#decimal handling
			ww += "point "
			ltpd = str(num)[int(str(num).index(".")) + 1:]	#math gives rounding errors, so string manipulation to get decimal
			if decAsLongWord:
				for digit in range(len(ltpd)):
					if ltpd[digit] == "0":
						ww += cword(int(ltpd[digit]),allowZero=True)
					else:
						break
				ww += cword(int(ltpd),allowZero=True)
			else:
				for digit in range(len(ltpd)):
					ww += cword(int(ltpd[digit]),allowZero=True)
			break
	return ww
		
def isFloat(s):
	try: float(s)
	except (ValueError, TypeError): return False
	else: return True

#example usage
number=''
while not isFloat(number):
	number = raw_input("Enter a number:")

print cword(float(number))
print cword(float(number),decAsLongWord=True)

Didn't add in handling for negative numbers, but that shouldn't be too difficult.

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top