Algorithm that converts numeric amount into Englis

2019-01-22 15:39发布

What is the most efficient way to convert numeric amount into English words

e.g. 12 to twelve 127 to one hundred twenty-seven

9条回答
Luminary・发光体
2楼-- · 2019-01-22 16:05

Note some rules:

  • Tens numbers (twenty, thirty, etc.) ending in y are followed by hyphens.
  • Teens are special (except 15-19, but they're still special).
  • Everything else is just some combination of digit place like "three thousand".

You can get the place of a number by using floor division of integers: 532 / 100 -> 5

查看更多
迷人小祖宗
3楼-- · 2019-01-22 16:11

This is part of Common Lisp!

Here's how GNU CLISP does it, and here's how CMUCL does it (easier to read, IMHO).

Doing a code search for "format million billion" will turn up lots of them.

查看更多
我命由我不由天
4楼-- · 2019-01-22 16:14

This is some old python code on my hard drive. There might be bugs but it should show the basic idea:

class Translator:
    def transformInt(self, x):
        translateNumbers(0,[str(x)])

    def threeDigitsNumber(self,number):
        snumber=self.twoDigitsNumber(number/100)
        if number/100!=0:
            snumber+=" hundred "
        snumber+self.twoDigitsNumber(number)
        return snumber+self.twoDigitsNumber(number)
    def twoDigitsNumber(self,number):
        snumber=""
        if number%100==10:
            snumber+="ten"
        elif number%100==11:
            snumber+="eleven"
        elif number%100==12:
            snumber+="twelve"
        elif number%100==13:
            snumber+="thirteen"
        elif number%100==14:
            snumber+="fourteen"
        elif number%100==15:
            snumber+="fifteen"
        elif number%100==16:
            snumber+="sixteen"
        elif number%100==17:
            snumber+="seventeen"
        elif number%100==18:
            snumber+="eighteen"
        elif number%100==19:
            snumber+="nineteen"
        else:
            if (number%100)/10==2:
                snumber+="twenty-"
            elif (number%100)/10==3:
                snumber+="thirty-"
            elif (number%100)/10==4:
                snumber+="forty-"
            elif (number%100)/10==5:
                snumber+="fifty-"
            elif (number%100)/10==6:
                snumber+="sixty-"
            elif (number%100)/10==7:
                snumber+="seventy-"
            elif (number%100)/10==8:
                snumber+="eighty-"
            elif (number%100)/10==9:
                snumber+="ninety-"
            if (number%10)==1:
                snumber+="one"
            elif (number%10)==2:
                snumber+="two"
            elif (number%10)==3:
                snumber+="three"
            elif (number%10)==4:
                snumber+="four"
            elif (number%10)==5:
                snumber+="five"
            elif (number%10)==6:
                snumber+="six"
            elif (number%10)==7:
                snumber+="seven"
            elif (number%10)==8:
                snumber+="eight"
            elif (number%10)==9:
                snumber+="nine"
            elif (number%10)==0:
                if snumber!="":
                    if snumber[len(snumber)-1]=="-":
                        snumber=snumber[0:len(snumber)-1]
        return snumber
    def translateNumbers(self,counter,words):
        if counter+1<len(words):
            self.translateNumbers(counter+1,words)
        else:
            if counter==len(words):
                return True
        k=0
        while k<len(words[counter]): 
            if (not (ord(words[counter][k])>47 and ord(words[counter][k])<58)):
                break
            k+=1
        if (k!=len(words[counter]) or k==0):
            return 1
        number=int(words[counter])
        from copy import copy
        if number==0:
            self.translateNumbers(counter+1,copy(words[0:counter]+["zero"]+words[counter+1:len(words)]))
            self.next.append(copy(words[0:counter]+["zero"]+words[counter+1:len(words)]))
            return 1
        if number<10000:
            self.translateNumbers(counter+1,copy(words[0:counter]
                                                      +self.seperatewords(self.threeDigitsNumber(number))
                                                      +words[counter+1:len(words)]))
            self.next.append(copy(words[0:counter]
                                       +self.seperatewords(self.threeDigitsNumber(number))
                                       +words[counter+1:len(words)]))
        if number>999:
            snumber=""
            if number>1000000000:
                snumber+=self.threeDigitsNumber(number/1000000000)+" billion "
                number=number%1000000000
            if number>1000000:
                snumber+=self.threeDigitsNumber(number/1000000)+" million "
                number=number%1000000
            if number>1000:
                snumber+=self.threeDigitsNumber(number/1000)+" thousand "
                number=number%1000
            snumber+=self.threeDigitsNumber(number)
            self.translateNumbers(counter+1,copy(words[0:counter]+self.seperatewords(snumber)
                                                      +words[counter+1:len(words)]))
            self.next.append(copy(words[0:counter]+self.seperatewords(snumber)
                                       +words[counter+1:len(words)]))
查看更多
登录 后发表回答