python: converting numbers to words

后端 未结 4 603
无人共我
无人共我 2021-01-13 23:08

I\'m trying to write a code that will convert numbers to words, up to 999 trillion. here is my code so far. it works up to 119, but after that things get messy. I can\'t use

4条回答
  •  借酒劲吻你
    2021-01-13 23:50

    I wrote this for project euler using lists originally and using python 2 . There may well be bugs I have not tested it too much but it should hopefully help you along

    nums = ["", "one", "two", "three", "four",  "five",
        "six", "seven", "eight", "nine "]
    teens = ["", "eleven", "twelve", "thirteen",  "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    tens = ["", "ten", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"]
    thousands = ["","thousand", "million",  "billion",  "trillion"]
    def num_to_words():
        n= int(input("Enter number to convert: "))
        words = ""
        if n == 0:
            words += "zero"
        else:
            numStr = "%d" % n
            groups = (len(numStr) + 2) // 3
            numStr = numStr.zfill(groups * 3)
            for i in range(0, groups*3, 3):
                h = int(numStr[i])
                t = int(numStr[i+1])
                u = int(numStr[i+2])
                g = groups - (i // 3 + 1)
                if h >= 1:
                    words += nums[h]
                    words +=  " hundred "
                    words+=" "
                    if int(numStr) % 100:   # if number  modulo 100 has remainder  add "and" i.e one hundred and ten
                        words+=" and "
                if t > 1:
                    words+= tens[t]
                    if u >= 1:
                        words+= nums[u]
                        words+=" "
                elif t == 1:
                    if u >= 1:
                        words+= teens[(u)]
                    else:
                        words+= tens[t]
                        words+=" "
                else:
                    if u >= 1:
                        words+= nums[u]
                        words+=" "
    
                if g >= 1 and (h + t + u) > 0:
                    words+= thousands[g]
                    words+=" "
        return words
    In [7]: num_to_words()
    Enter number to convert: 12399990
    Out[7]: 'twelvemillion three hundred   and ninetynine  thousand nine  hundred   and ninety'
    

提交回复
热议问题