0

Without using library function how can you print any number of words in Python? there are some answers were using library function but I want the core code.

Like:
    12345 = "twelve thousand three hundred and forty five"
    97835200 ="Nine core seventy eight lakh thirty five thousand two hundred"
    230100 = "Two lakh thirty thousand one hundred"
Kousik
  • 21,485
  • 7
  • 36
  • 59

3 Answers3

5

Code for this:


>>>def handel_upto_99(number):
predef={0:"zero",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",20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety",100:"hundred",100000:"lakh",10000000:"crore",1000000:"million",1000000000:"billion"}
if number in predef.keys():
    return predef[number]
else:
    return predef[(number/10)*10]+' '+predef[number%10]

>>>def return_bigdigit(number,devideby):
predef={0:"zero",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",20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety",100:"hundred",1000:"thousand",100000:"lakh",10000000:"crore",1000000:"million",1000000000:"billion"}
if devideby in predef.keys():
    return predef[number/devideby]+" "+predef[devideby]
else:
    devideby/=10
    return handel_upto_99(number/devideby)+" "+predef[devideby]

>>>def mainfunction(number):
dev={100:"hundred",1000:"thousand",100000:"lakh",10000000:"crore",1000000000:"billion"}
if number is 0:
    return "Zero"
if number<100:
    result=handel_upto_99(number)

else:
    result=""
    while number>=100:
        devideby=1
        length=len(str(number))
        for i in range(length-1):
            devideby*=10
        if number%devideby==0:
            if devideby in dev:
                return handel_upto_99(number/devideby)+" "+ dev[devideby]
            else:
                return handel_upto_99(number/(devideby/10))+" "+ dev[devideby/10]
        res=return_bigdigit(number,devideby)
        result=result+' '+res
        if devideby not in dev:
            number=number-((devideby/10)*(number/(devideby/10)))
        number=number-devideby*(number/devideby)

    if number <100:
        result = result + ' '+ handel_upto_99(number)
return result

Copy the three function one by one and paste in your python shell. after that run like this:

ANSWER:

>>>mainfunction(12345)
' twelve thousand three hundred forty five'

>>>mainfunction(0)
'Zero'

>>>mainfunction(100)
'one hundred'

>>>mainfunction(40230534)
' four crore two lakh thirty thousand five hundred thirty four'
Kousik
  • 21,485
  • 7
  • 36
  • 59
3

you cam use third party library num2word available in python

num2word.to_card(1e25)
'ten septillion, one billion, seventy-three million, seven hundred and forty-one


this will avoid your long code and you can directly use it.
Alok Agarwal
  • 3,071
  • 3
  • 23
  • 33
1

Below is a function that can convert numbers into words. It uses the standard English name for numbers, but you can modify it for your special names if you need. This function can handle up to 10^60 numbers. Use it by calling the function: int2word(n) where n is the number

def int2word(n):
"""
convert an integer number n into a string of english words
"""
# break the number into groups of 3 digits using slicing
# each group representing hundred, thousand, million, billion, ...
n3 = []
r1 = ""
# create numeric string
ns = str(n)
for k in range(3, 33, 3):
    r = ns[-k:]
    q = len(ns) - k
    # break if end of ns has been reached
    if q < -2:
        break
    else:
        if  q >= 0:
            n3.append(int(r[:3]))
        elif q >= -1:
            n3.append(int(r[:2]))
        elif q >= -2:
            n3.append(int(r[:1]))
    r1 = r

#print n3  # test

# break each group of 3 digits into
# ones, tens/twenties, hundreds
# and form a string
nw = ""
for i, x in enumerate(n3):
    b1 = x % 10
    b2 = (x % 100)//10
    b3 = (x % 1000)//100
    #print b1, b2, b3  # test
    if x == 0:
        continue  # skip
    else:
        t = thousands[i]
    if b2 == 0:
        nw = ones[b1] + t + nw
    elif b2 == 1:
        nw = tens[b1] + t + nw
    elif b2 > 1:
        nw = twenties[b2] + ones[b1] + t + nw
    if b3 > 0:
        nw = ones[b3] + "hundred " + nw
return nw

'''Global'''

ones = ["", "one ","two ","three ","four ", "five ",
"six ","seven ","eight ","nine "]

tens = ["ten ","eleven ","twelve ","thirteen ", "fourteen ",
"fifteen ","sixteen ","seventeen ","eighteen ","nineteen "]

twenties = ["","","twenty ","thirty ","forty ",
"fifty ","sixty ","seventy ","eighty ","ninety "]

thousands = ["","thousand ","million ", "billion ", "trillion ",
"quadrillion ", "quintillion ", "sextillion ", "septillion ","octillion ",
"nonillion ", "decillion ", "undecillion ", "duodecillion ", "tredecillion ",
"quattuordecillion ", "sexdecillion ", "septendecillion ", "octodecillion ",
"novemdecillion ", "vigintillion "]
GrimPython
  • 13
  • 4
  • 1
    source: [Number to Word Converter (Python)](http://www.daniweb.com/software-development/python/code/216839/number-to-word-converter-python) – Ashwini Chaudhary Apr 25 '13 at 14:36