[Tutor] Text numerals?

Don Arnold darnold02 at sprynet.com
Sun May 16 01:42:15 EDT 2004

```
> Is there a function that will return 'one' for 1, 'two' for two, etc.?
>

I assume you mean 'two' for 2 ;).  But no, AFAIK there is no stdlib
implementation.

<snip dictionary suggestions>

The problem of converting full-blown numbers of any length into letters is
much harder.  Some years ago I remember this being coded in python; if the

But make sure you need that kind of complexity; better to have a solution
you understand than one which is too powerful that you don't.  It helps when
things go wrong ;).

--
Glen

Good advice, but this problem turned out not to be as complex as it first
appeared. Really, all you need to do is pad the number to a length that's
divisible by 3 and then start processing digits from the left, three at a
time. Here's my take on it:

littleNumbers = {'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'
}

tens = {'2': 'twenty', '3': 'thirty', '4': 'forty', '5': 'fifty',
'6': 'sixty', '7': 'seventy', '8': 'eighty', '9': 'ninety'
}

groupings = ['decillion', 'nonillion', 'octillion',
'septillion', 'sextillion', 'quintillion',
'thousand', ''
]

def process(num):
maxDigits = 36
result = []
group = 0

if num < 0:
result.append('negative')
num = abs(num)

num = str(num).zfill(maxDigits)

if len(num) > maxDigits:
raise 'Number too large'

for i in range(0, maxDigits - 1, 3):
chunk = num[i:i+3]
if chunk != '000':
if chunk != '0':
result.append(littleNumbers[chunk])
result.append('hundred')

if chunk == '1':
result.append(littleNumbers[chunk[1:3]])
else:
tensString = ''
if chunk != '0':
if chunk != '0':
tensString += tens[chunk] + '-'
else:
tensString += tens[chunk]

if chunk != '0':
tensString += littleNumbers[chunk]

if tensString:
result.append(tensString)

if groupings[group]:
result.append(groupings[group] + ',')
group += 1

if not result:
return 'zero'
else:
result = ' '.join(result)

if result.endswith(','):
result = result[:-1]

return result

def main():
while 1:
print
num = int(raw_input('Enter an integer (0 to quit): '))
print process(num)
if num == 0:
break

if __name__ == '__main__':
main()

This should handle any integer up to 36 digits long. Here's a sample run:

Enter an integer (0 to quit): 10
ten

Enter an integer (0 to quit): 243000
two hundred forty-three thousand

Enter an integer (0 to quit): 17
seventeen

Enter an integer (0 to quit): 232893
two hundred thirty-two thousand, eight hundred ninety-three

Enter an integer (0 to quit): -12
negative twelve

Enter an integer (0 to quit): -1456
negative one thousand, four hundred fifty-six

Enter an integer (0 to quit): 123456789012345678901234567890123456
one hundred twenty-three decillion, four hundred fifty-six nonillion, seven
hundred eighty-nine octillion, twelve septillion, three hundred forty-five
sextillion, six hundred seventy-eight quintillion, nine hundred one
quadrillion, two hundred thirty-four trillion, five hundred sixty-seven
billion, eight hundred ninety million, one hundred twenty-three thousand,
four hundred fifty-six

Enter an integer (0 to quit): 0
zero

HTH,
Don

```