DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert An Integer To English Written Form

09.13.2005
| 8919 views |
  • submit to reddit
        English is a peculiar language.  english_number() takes an integer, and returns the long, written form most famously used on checks and the like.  Algorithm inspired by some old HP online docs.

to_19 = ( 'zero',  'one',   'two',  'three', 'four',   'five',   'six',
          'seven', 'eight', 'nine', 'ten',   'eleven', 'twelve', 'thirteen',
          'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' )
tens  = ( 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety')
denom = ( '',
          'thousand',     'million',         'billion',       'trillion',       'quadrillion',
          'quintillion',  'sextillion',      'septillion',    'octillion',      'nonillion',
          'decillion',    'undecillion',     'duodecillion',  'tredecillion',   'quattuordecillion',
          'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' )

# convert a value < 100 to English.
def _convert_nn(val):
    if val < 20:
        return to_19[val]
    for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
        if dval + 10 > val:
            if val % 10:
                return dcap + '-' + to_19[val % 10]
            return dcap

# convert a value < 1000 to english, special cased because it is the level that kicks 
# off the < 100 special case.  The rest are more general.  This also allows you to
# get strings in the form of 'forty-five hundred' if called directly.
def _convert_nnn(val):
    word = ''
    (mod, rem) = (val % 100, val // 100)
    if rem > 0:
        word = to_19[rem] + ' hundred'
        if mod > 0:
            word = word + ' '
    if mod > 0:
        word = word + _convert_nn(mod)
    return word

def english_number(val):
    if val < 100:
        return _convert_nn(val)
     if val < 1000:
         return _convert_nnn(val)
    for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
        if dval > val:
            mod = 1000 ** didx
            l = val // mod
            r = val - (l * mod)
            ret = _convert_nnn(l) + ' ' + denom[didx]
            if r > 0:
                ret = ret + ', ' + english_number(r)
            return ret

    

Comments

Snippets Manager replied on Sun, 2010/09/19 - 4:20pm

Hy, I need some help. I try tu use this code into an HTML Template output of Checkout program (http://checkoutapp.com/documentation/94_howto_template), I really not programer but I know little PHP. I need this for tu print in the bills and I will translate in Spanish. Maybe can hep ! I did this is not working: #set val = $totalPriceIn #set r = '' #set ret = '' #set rem ='' #set mod = '' #set word = '' #set to_19 = ( 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ) #set tens = ( 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety') #set denom = ( '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' ) #def _convert_nn(val): #if val < 20: #return to_19[val] #end if #end def #for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)): #if dval + 10 > val: #if val % 10: #return dcap + '-' + to_19[val % 10] #return dcap #end if #end if #end for #def _convert_nnn(val): word = '' (mod, rem) = (val % 100, val // 100) #end def #if rem > 0: word = to_19[rem] + ' hundred' #if mod > 0: word = word + ' ' #end if #end if #if mod > 0: word = word + _convert_nn(mod) #return word #end if #def english_number(val): #if val < 100: #return _convert_nn(val) #if val < 1000: #return _convert_nnn(val) #end if #end if #end def #for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))): #if dval > val: #mod = 1000 ** didx #l = val // mod #r = val - (l * mod) #ret = _convert_nnn(l) + ' ' + denom[didx] #if r > 0: #ret = ret + ', ' + english_number(r) #return ret #end if #end if #end for #set $imp = ret $imp