How to convert an integer number to words in python?

前端 未结 6 602
走了就别回头了
走了就别回头了 2021-01-21 05:09

Write a function that takes an integer as input argument and returns the integer using words. For example if the input is 4721 then the function should retur

相关标签:
6条回答
  • 2021-01-21 05:37
    def number_to_words(number):
        names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
        lst = []
        while True:
            lst.insert(0, number % 10) # Prepend
            number /= 10
            if number == 0:    # Do this here rather than in the while condition
               break;          # so that 0 results in ["zero"]
    
        # If you want a list, then:
        return lst;
    
        # If you want a string, then:
        return " ".join(lst)       
    
    0 讨论(0)
  • 2021-01-21 05:39

    I would use re.sub

    >>> def Numbers_To_Words (number):
        dict_ = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six", '7': "seven", '8': "eight", '9': "nine", '0': "zero"}
        return re.sub(r'\d', lambda m: dict_[m.group()] + ' ', str(number)).strip()
    
    >>> print Numbers_To_Words(4721)
    four seven two one
    >>> print Numbers_To_Words(98765423)
    nine eight seven six five four two three
    
    0 讨论(0)
  • 2021-01-21 05:43
    def number_to_words(number):
        dict={"1":"one","2":"two","3":"three","4":"four","5":"five","6":"six","7":"seven","8":"eight","9":"nine","0":"zero"}
        s=""
        for c in str(number):
            s+=dict[c]+" "
        #if it's matter that the string won't conatain
        #space at the end then add the next line:
        s=s[:-1]
    
        return s
    
    0 讨论(0)
  • 2021-01-21 05:44

    This will help you where you can't import modules for converting numbers to words. This code is for converting numbers from 1 to 1000 both inclusive.

    def integer_to_english(number):
        if number>=1 and number<=1000:
            a = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty ','thirty ','fourty ','fifty ','sixty ','seventy ','eighty ','ninty ']
            if number<=20:
                if number%10==0: return a[number]
                else: return a[number]
            elif number<100:
                b=number-20
                r=b%10
                b//=10
                return a[20+b]+a[r]
            elif number<1000:
                if number%100==0:
                    b=number//100
                    return a[b]+' hundred'
                else:
                    r=number%100
                    b=number//100
                    if r<=20:
                        return a[b]+' hundred'+' and '+a[r]
                    else:
                        r=r-20
                        d=r//10
                        r%=10
                        return a[b]+' hundred'+' and '+a[20+d]+a[r]
            elif number==1000:
                return 'one thousand'
            else:
                return -1
    
    number=789
    print(integer_to_english(number))
    
    0 讨论(0)
  • 2021-01-21 05:46

    There is a way that is simpler than the rest:

    def number_to_words(number)
        words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
        return " ".join(words[int(i)] for i in str(number))
    
    0 讨论(0)
  • 2021-01-21 05:54

    Use modules for it: https://pypi.python.org/pypi/num2words

    Also, check similar questions: How do I tell Python to convert integers into words

    You can install the modules and see how it is implemented there.

    But your problem is solved like:

    def Numbers_To_Words (number):
        dictionary = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six",
                '7': "seven", '8': "eight", '9': "nine", '0': "zero"}
        return " ".join(map(lambda x: dictionary[x], str(number)))
    
    
    print Numbers_To_Words(1234)
    
    0 讨论(0)
提交回复
热议问题