Integer to English

TrustyTony 0 Tallied Votes 1K Views Share

Here is my try of changing integers to English written numbers.

Here corrected version 3 check:

from __future__ import print_function
import random,sys
if sys.version_info[0]==3:
    raw_input=input
    
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"]


def log1000(n):
    " integer log1000 "
    res=0
    while n>1:
        res+=1
        n/=1000.0
    return res

def inttoeng(number):
    unit=log1000(number)-1
    if unit>0:
        unit_amount,number=divmod(number,1000**unit)
        res=(inttoeng(unit_amount)+' '+ denom[unit]+' ')
        res+='and ' + inttoeng(number) if number<100 else '' + inttoeng(number)
        return res
    else:
        hundreds,under_100 = divmod(number,100)
        ten,unit = divmod(under_100,10)
        res=inttoeng(hundreds)+ ' hundred ' if hundreds else ''
        if not under_100:
            return res
        res+='and ' if hundreds and under_100 else ''
        res+= tens[ten] if ten>1 else to_19[under_100]
        res+= '-'+to_19[unit] if ten>1 and unit else ''
        return res
        
if __name__=='__main__':

    for i in range(20):
        num = random.randint(0,1e21)
        print(num, '=',end=' ')
        print(inttoeng(num))
    raw_input('Push enter to finish')
from __future__ import print_function
import random
if sys.version_info[0]=3:
    raw_input=input
    
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"]


def log1000(n):
    " integer log10 "
    res=0
    while n>1:
        res+=1
        n/=1000.0
    return res

def inttoeng(number):
    unit=log1000(number)-1
    if unit>0:
        unit_amount,number=divmod(number,1000**unit)
        res=(inttoeng(unit_amount)+' '+ denom[unit]+' ')
        res+='and ' + inttoeng(number) if number<100 else '' + inttoeng(number)
        return res
    else:
        hundreds,under_100 = divmod(number,100)
        ten,unit = divmod(under_100,10)
        res=inttoeng(hundreds)+ ' hundred ' if hundreds else ''
        if not under_100:
            return res
        res+='and ' if hundreds and under_100 else ''
        res+= tens[ten] if ten>1 else to_19[under_100]
        res+= '-'+to_19[unit] if ten>1 and unit else ''
        return res
        
if __name__=='__main__':

    for i in range(20):
        num = random.randint(0,1e21)
        print(num, '=',end=' ')
        print(inttoeng(num))
    raw_input('Push enter to finish')
TrustyTony 888 pyMod Team Colleague Featured Poster

I changed logarithm function to be log1000 instead of log 10 to get rid of dividing by 3 by putting while test to test <1 instead of <10, therefore giving one too big number, which I fixed where I used it at line 26. Proper fix of lines 17 until 26 is:

def log1000(n):
    " integer log10 "
    res=0
    while n>1000:
        res+=1
        n/=1000.0
    return res

def inttoeng(number):
    unit=log1000(number)

ie test <1000 and remove the -1 from line 26. (the doc string I fixed in my resend of the code in main message.

To get some more variation of the test numbers use this random number instead (it produce logarithmic random numbers):

num = int(random.randint(0,10**random.randint(5,50))*random.random())

Example output:

29448400668950347776 = twenty-nine quintillion four hundred and forty-eight quadrillion four hundred  trillion six hundred and sixty-eight billion nine hundred and fifty million three hundred and forty-seven thousand seven hundred and seventy-six
285115824111775968871981990491744166442172416 = two hundred and eighty-five tredecillion one hundred and fifteen duodecillion eight hundred and twenty-four undecillion one hundred and eleven decillion seven hundred and seventy-five nonillion nine hundred and sixty-eight octillion eight hundred and seventy-one septillion nine hundred and eighty-one sextillion nine hundred and ninety quintillion four hundred and ninety-one quadrillion seven hundred and forty-four trillion one hundred and sixty-six billion four hundred and forty-two million one hundred and seventy-two thousand four hundred and sixteen
3828698140811058395044712512066695599628325224448 = three sexdecillion eight hundred and twenty-eight quattuordecillion six hundred and ninety-eight tredecillion one hundred and forty duodecillion eight hundred and eleven undecillion fifty-eight decillion three hundred and ninety-five nonillion forty-four octillion seven hundred and twelve septillion five hundred and twelve sextillion sixty-six quintillion six hundred and ninety-five quadrillion five hundred and ninety-nine trillion six hundred and twenty-eight billion three hundred and twenty-five million two hundred and twenty-four thousand four hundred and forty-eight
21365795978941889775263341626022428672 = twenty-one undecillion three hundred and sixty-five decillion seven hundred and ninety-five nonillion nine hundred and seventy-eight octillion nine hundred and forty-one septillion eight hundred and eighty-nine sextillion seven hundred and seventy-five quintillion two hundred and sixty-three quadrillion three hundred and forty-one trillion six hundred and twenty-six billion twenty-two million four hundred and twenty-eight thousand six hundred and seventy-two
263192636 = two hundred and sixty-three million one hundred and ninety-two thousand six hundred and thirty-six
1232547858116767488 = one quintillion two hundred and thirty-two quadrillion five hundred and forty-seven trillion eight hundred and fifty-eight billion one hundred and sixteen million seven hundred and sixty-seven thousand four hundred and eighty-eight
13625730495245612744704 = thirteen sextillion six hundred and twenty-five quintillion seven hundred and thirty quadrillion four hundred and ninety-five trillion two hundred and forty-five billion six hundred and twelve million seven hundred and forty-four thousand seven hundred and four
48118762352815151180836382834141768417231765504 = forty-eight quattuordecillion one hundred and eighteen tredecillion seven hundred and sixty-two duodecillion three hundred and fifty-two undecillion eight hundred and fifteen decillion one hundred and fifty-one nonillion one hundred and eighty octillion eight hundred and thirty-six septillion three hundred and eighty-two sextillion eight hundred and thirty-four quintillion one hundred and forty-one quadrillion seven hundred and sixty-eight trillion four hundred and seventeen billion two hundred and thirty-one million seven hundred and sixty-five thousand five hundred and four
318991806133875 = three hundred and eighteen trillion nine hundred and ninety-one billion eight hundred and six million one hundred and thirty-three thousand eight hundred and seventy-five
19449 = nineteen thousand four hundred and forty-nine
1708962342763227894581905051738963968 = one undecillion seven hundred and eight decillion nine hundred and sixty-two nonillion three hundred and forty-two octillion seven hundred and sixty-three septillion two hundred and twenty-seven sextillion eight hundred and ninety-four quintillion five hundred and eighty-one quadrillion nine hundred and five trillion fifty-one billion seven hundred and thirty-eight million nine hundred and sixty-three thousand nine hundred and sixty-eight
4083643086383767939036582248448 = four nonillion eighty-three octillion six hundred and forty-three septillion eighty-six sextillion three hundred and eighty-three quintillion seven hundred and sixty-seven quadrillion nine hundred and thirty-nine trillion thirty-six billion five hundred and eighty-two million two hundred and forty-eight thousand four hundred and forty-eight
681375464 = six hundred and eighty-one million three hundred and seventy-five thousand four hundred and sixty-four
209513215735550115471874326528 = two hundred and nine octillion five hundred and thirteen septillion two hundred and fifteen sextillion seven hundred and thirty-five quintillion five hundred and fifty quadrillion one hundred and fifteen trillion four hundred and seventy-one billion eight hundred and seventy-four million three hundred and twenty-six thousand five hundred and twenty-eight
34327188 = thirty-four million three hundred and twenty-seven thousand one hundred and eighty-eight
6110267992882532211865072117729301270986259169280 = six sexdecillion one hundred and ten quattuordecillion two hundred and sixty-seven tredecillion nine hundred and ninety-two duodecillion eight hundred and eighty-two undecillion five hundred and thirty-two decillion two hundred and eleven nonillion eight hundred and sixty-five octillion seventy-two septillion one hundred and seventeen sextillion seven hundred and twenty-nine quintillion three hundred and one quadrillion two hundred and seventy trillion nine hundred and eighty-six billion two hundred and fifty-nine million one hundred and sixty-nine thousand two hundred and eighty
4676490760352434453759799990142631936 = four undecillion six hundred and seventy-six decillion four hundred and ninety nonillion seven hundred and sixty octillion three hundred and fifty-two septillion four hundred and thirty-four sextillion four hundred and fifty-three quintillion seven hundred and fifty-nine quadrillion seven hundred and ninety-nine trillion nine hundred and ninety billion one hundred and forty-two million six hundred and thirty-one thousand nine hundred and thirty-six
5095384409942373512972937569068415713280000 = five tredecillion ninety-five duodecillion three hundred and eighty-four undecillion four hundred and nine decillion nine hundred and forty-two nonillion three hundred and seventy-three octillion five hundred and twelve septillion nine hundred and seventy-two sextillion nine hundred and thirty-seven quintillion five hundred and sixty-nine quadrillion sixty-eight trillion four hundred and fifteen billion seven hundred and thirteen million two hundred and eighty thousand and 
770296816385750470990657103325904932700160 = seven hundred and seventy duodecillion two hundred and ninety-six undecillion eight hundred and sixteen decillion three hundred and eighty-five nonillion seven hundred and fifty octillion four hundred and seventy septillion nine hundred and ninety sextillion six hundred and fifty-seven quintillion one hundred and three quadrillion three hundred and twenty-five trillion nine hundred and four billion nine hundred and thirty-two million seven hundred  thousand one hundred and sixty
113496428628 = one hundred and thirteen billion four hundred and ninety-six million four hundred and twenty-eight thousand six hundred and twenty-eight
Push enter to finish

Sorry for the "dirty code".

TrustyTony 888 pyMod Team Colleague Featured Poster

Bug fixes: would not print 'zero', equality missing from log1000 (therefore ten hundred)

Negative numbers added.

Explain function is useful help function, even very simple.

QUIZ: what number are these relative numbers?

'one million forty-eight thousand five hundred and seventy-six'

'eighteen quintillion four hundred and forty-six quadrillion seven hundred and forty-four trillion seventy-three billion seven hundred and nine million five hundred and fifty-one thousand six hundred and sixteen'

from __future__ import print_function
import random,sys
if sys.version_info[0]==3:
    raw_input=input
    
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"]


def log1000(n):
    " integer log10 "
    res=0
    while n>=1000:
        res+=1
        n/=1000.0
    return res

def inttoeng(number):
    if number<0:
        return 'minus '+inttoeng(-number)
    unit=log1000(number)
    if unit:
        unit_amount,number=divmod(number,1000**unit)
        res=(inttoeng(unit_amount)+' '+ denom[unit])
        if number:
            res+=' and ' + inttoeng(number) if number<100  else ' ' + inttoeng(number)
        return res
    else:
        if number<20:
            return to_19[number]
        hundreds,under_100 = divmod(number,100)
        ten,unit = divmod(under_100,10)
        res=inttoeng(hundreds)+ ' hundred ' if hundreds else ''
        if not under_100:
            return res
        res+='and ' if hundreds and under_100 else ''
        res+= tens[ten] if ten>1 else to_19[under_100]
        res+= '-'+to_19[unit] if ten>1 and unit else ''
        return res
    
def explain(number):
    return "%s (%s) " % (number,inttoeng(number))

if __name__=='__main__':
    for num in range(-700,10000,700):
        print(explain(num))
    raw_input('Push enter to finish')
""" Output:
-700 (minus seven hundred ) 
0 (zero) 
700 (seven hundred ) 
1400 (one thousand four hundred ) 
2100 (two thousand one hundred ) 
2800 (two thousand eight hundred ) 
3500 (three thousand five hundred ) 
4200 (four thousand two hundred ) 
4900 (four thousand nine hundred ) 
5600 (five thousand six hundred ) 
6300 (six thousand three hundred ) 
7000 (seven thousand) 
7700 (seven thousand seven hundred ) 
8400 (eight thousand four hundred ) 
9100 (nine thousand one hundred ) 
9800 (nine thousand eight hundred ) 
Push enter to finish
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.