Beruflich Dokumente
Kultur Dokumente
# conversion mappings
_1to9Dict = {'0':'', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five',
'6':'six', '7':'seven', '8':'eight', '9':'nine'}
_10to19Dict = {'0':'ten', '1':'eleven', '2':'twelve', '3':'thirteen', '4':'fourt
een', '5':'fifteen', '6':'sixteen',
'7':'seventeen', '8':'eighteen', '9':'nineteen'}
_20to90Dict = {'2':'twenty', '3':'thirty', '4':'forty', '5':'fifty', '6':'sixty'
, '7':'seventy', '8':'eighty', '9':'ninety'}
def num2words(numString):
if numString == '0': return('zero')
if len(numString) > 2: return "Sorry can't handle numbers with more than 2 d
igits"
numString = '0' + numString
# pad on left in case its a single digit num
ber
tens, ones = numString[-2], numString[-1]
if tens == '0': return _1to9Dict[ones]
elif tens == '1': return _10to19Dict[ones]
else: return _20to90Dict[tens] + ' ' + _1to9Dict[ones]
def main():
print num2words(sys.argv[1])
main()
------------------------------- File n2w.py ------------------------------#! /usr/bin/env python
"""n2w: number to words conversion module: contains function num2words. Can also
be run as a script
usage as a script: n2w num
(Convert a number to its English word description)
num: whole integer from 0 and 999,999,999,999,999 (commas are optional)
example: n2w 10,103,103
for 10,003,103 say: ten million three thousand one hundred three
"""
import sys, string
# conversion mappings
_1to9Dict = {'0':'', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five',
'6':'six', '7':'seven', '8':'eight', '9':'nine'}
_10to19Dict = {'0':'ten', '1':'eleven', '2':'twelve', '3':'thirteen', '4':'fourt
een', '5':'fifteen', '6':'sixteen',
'7':'seventeen', '8':'eighteen', '9':'nineteen'}
_20to90Dict = {'2':'twenty', '3':'thirty', '4':'forty', '5':'fifty', '6':'sixty'
, '7':'seventy', '8':'eighty', '9':'ninety'}
_magnitudeList = [(0,''), (3,' thousand '), (6, ' million '), (9, ' billion '),
(12, ' trillion '),(15,'')]
def num2words(numString):
"""num2words(numString): converts number represented by string to English wo
rds"""
# handle the special conditions (number is zero or too large)
if numString == '0': return('zero')
numLength = len(numString)
maxDigits = _magnitudeList[-1][0]
if numLength > maxDigits:
return "Sorry, can't handle numbers with more than %d digits" % (maxDig
its)
# working from least to most significant digit create a string containing t
he number
numString = '00' + numString
# pad the number on the left
wordString = ''
# initiate string for number
for mag, name in _magnitudeList:
if mag >= numLength: return wordString
else:
hundreds, tens, ones = numString[-mag-3], numString[-mag-2], numStri
ng[-mag-1]
if not (hundreds == tens == ones == '0'):
wordString = _handle1to999(hundreds, tens, ones) + name + wordS
tring
def _handle1to999(hundreds, tens, ones):
if hundreds == '0': return _handle1to99(tens,ones)
else: return _1to9Dict[hundreds] + ' hundred ' + _handle1to99(tens,ones)
def _handle1to99(tens, ones):
if tens == '0': return _1to9Dict[ones]
elif tens == '1': return _10to19Dict[ones]
else: return _20to90Dict[tens] + ' ' + _1to9Dict[ones]
def test():
values = string.split(sys.stdin.read())
for val in values:
num = string.replace(val, ',', '')
print "%s = %s" % ( val, num2words(num) )
def main():
if len(sys.argv) != 2 or sys.argv[1] == '+?':
print __doc__
elif sys.argv[1] == '+test':
test()
else:
num = string.replace(sys.argv[1], ',', '')
try:
result = num2words(num)
except KeyError:
print __doc__
else:
print "For %s, say: %s" % (sys.argv[1], result)
if __name__ == '__main__':
main()
else:
print "n2w loaded as a module"