×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# number to english equivalent

## number to english equivalent

(OP)
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.

### RE: number to english equivalent

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.

### RE: number to english equivalent

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])

### RE: number to english equivalent

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.

### RE: number to english equivalent

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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!