How to convert an integer number to words in python?

前端 未结 6 675
走了就别回头了
走了就别回头了 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)       
    

提交回复
热议问题