number to words converter

john conrad curt at journyx.com
Wed Apr 4 21:13:58 EDT 2001


i had fun wth this...


-------------- next part --------------
""" Just run it, you'll see what it does.

This code is released into the public domain absolutely free by http://journyx.com
as long as you keep this comment on the document and all derivatives of it.

"""

def getfractionwords(num):
        frac = num-int(num)
        numstr = str(int(frac*100+.5))
        if len(numstr) == 1:
                numstr = '0'+numstr
        fracstr = ' and ' + numstr + '/100'
        return fracstr

def convertDigit(digit):
        digits = ('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
        return digits[int(digit+.5)]

def breakintochunks(num):
        (left,right) = breakintwo(num)
        rv = [right]
        while left > 999:
                (left,right) = breakintwo(left)
                rv.append(right)
        rv.append(left)
        rv.reverse()
        return rv

def breakintwo(num):
        leftpart = int(num/1000.0)+0.0
        rightpart = 1000.0*(num/1000.0 - leftpart)
        return (int(leftpart+.5),int(rightpart+.5))

def enties(num):
        tens = ('','','twenty' ,'thirty', 'forty', 'fifty', 'sixty',
                'seventy', 'eighty', 'ninety')
        indx = int(num/10.0)
        return tens[indx]

def convert2digit(num):
        teens = ('ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
                 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen')
        if num < 10:
                return convertDigit(num)
        if num <20:
                return teens[num-10]
        if num > 19:
                tens = enties(num)
                ones = convertDigit(10*(num/10.0-int(num/10.0)))
                if ones:
                        rv= tens+'-'+ones
                else:
                        rv=tens
                return rv

def convert3digit(num):
        threenum = str(num)
        ln = len(threenum)
        if ln==3 :
                a= convertDigit(int(threenum[0]))
                b= ' hundred '
                c=     convert2digit(int(threenum[1:]))
                return a+b+c
        if ln<3 :
                return convert2digit(int(threenum))
        raise 'bad num',num

def num2words(num):
        thousandchunks = breakintochunks(int(num))
        rv = ' '
        if num >= 1000000:
                rv=rv+ convert3digit(thousandchunks[-3])+ ' million '
        if num >= 1000:
                c3d= convert3digit(thousandchunks[-2])
                if c3d:
                        rv=rv+ c3d+ ' thousand '
        rv = rv + convert3digit(thousandchunks[-1])+ getfractionwords(num)
        return squishWhiteSpace(rv)

def squishWhiteSpace(strng):
        """ Turn 2 spaces into one, and get rid of leading and trailing
            spaces. """
        import string,re
        return string.strip(re.sub('[ \t\n]+', ' ', strng))

def main():
        for i in range(1,111,7):
                print i,num2words(i)

        for i in (494.15, 414.90, 499.35, 400.98, 101.65, 110.94, \
                  139.85, 12349133.40, 2309033.75, 390313.41, 99390313.15, \
                  14908.05, 10008.49, 100008.00, 1000008.00, 100000008.00, \
                  14900.05, 10000.49, 100000.00, 1000000.00, 100000000.00, 8.49):
                print i,num2words(i)

        import whrandom
        for i in range(33):
                num = whrandom.randint(1,999999999) + whrandom.randint(1,99)/100.0
                print num,num2words(num)

if __name__ == '__main__':
        main()



More information about the Python-list mailing list