-1

I need to build a function that takes word representation of a number as an input and transforms it into a digit. one => 1, threethousandsixtyhundreadtwo => 3602 etc. I have the upper limit for the number (999,999) and I have already done reverse function. I have also all the words that will be used. So far, I am stuck on, how to separate each part of the number, when there are no spaces or anything similar between different parts of the number.

Doing this in python. I didn't find any reasonable method to do it with find().

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • 3
    Do you have to code this yourself? (See [word2number](https://pypi.org/project/word2number/).) If yes, please be more specific about where you are stuck in your implementation (post the relevant code). – timgeb Oct 21 '19 at 11:23
  • 1
    There is also [text2digits](https://github.com/careless25/text2digits) as an alternative. – jdehesa Oct 21 '19 at 11:28

1 Answers1

1

Use this code:

def int2word(number):
    def num2word(num):
        n = ["one",
              "two",
              "three",
              "four",#lists
              "five",
              "six",
              "seven",
              "eight",
              "nine",
              "ten",
              "eleven",
              "twelve",
              "thirteen",
              "fourteen",
              "fifteen",
              "sixteen",
              "seventeen",
              "eighteen",
              "nineteen"]
        ten = [ "twenty",
                 "thirty",
                 "forty",
                 "fifty",
                 "sixty",
                 "seventy",
                 "eighty",
                 "ninety",
                 ]
        if num == 0:
            return ""
        elif num in range(1, 19):#for nos 0 - 19
            return  (n[num-1])
        elif num in range(20,99) and num%10 == 0:#for nos 20 - 99 that are multiples of ten
            return (ten[num//10 - 2])
        elif num in range(20,99)and num%10 > 0:
            return (ten[num//10-2] +"-"+ n[num%10-1])
    result = ""
    if  number == 0:
        result = "zero"
    else:
        c = number // 10000000
        if c !=  0:
            crores = num2word(c)
            result += crores + " crore "
        l = number % 10000000
        if l//100000 != 0:
            lakh = num2word(l//100000)
            result += lakh + " lakh "
        th = number%100000
        if th//1000 != 0:
            thos = num2word(th//1000)
            result += thos + " thousand "
        h = number%1000
        if h//100 != 0:
            huns = num2word(h//100)
            result += huns + " hundred "
        t = h % 100
        if t != 0:
            tens = num2word(t)
            result += tens
    return result
Sid
  • 2,174
  • 1
  • 13
  • 29