用于将数字转换为文字以打印支票的Python脚本

用于将数字转换为文字以打印支票的Python脚本,python,Python,我想我可以避免在这里重新发明轮子 我需要一个Python脚本来将数字转换为文字以打印支票 例如,1,10543应以One lac一万五千五百四十三个的形式提供输出编写了具有以下功能的自定义转换器: 可用于0到99999999之间数字的数字转换器 面向印度次大陆,即拉丁美洲和克罗尔(范围较大 足以容纳大量用例) 包括paisa支持,最多2位小数(四舍五入) 灵感来源于 分析信息:该脚本的执行时间为0.458秒,而上述脚本的执行时间为0.237秒,仅运行10000次 class Number2Wo

我想我可以避免在这里重新发明轮子

我需要一个Python脚本来将数字转换为文字以打印支票


例如,
1,10543
应以
One lac一万五千五百四十三个
的形式提供输出

编写了具有以下功能的自定义转换器:

  • 可用于0到99999999之间数字的数字转换器 面向印度次大陆,即拉丁美洲和克罗尔(范围较大 足以容纳大量用例)
  • 包括paisa支持,最多2位小数(四舍五入)
  • 灵感来源于
  • 分析信息:该脚本的执行时间为0.458秒,而上述脚本的执行时间为0.237秒,仅运行10000次

    class Number2Words(object):
    
            def __init__(self):
                '''Initialise the class with useful data'''
    
                self.wordsDict = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven',
                                  8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen',
                                  14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen',
                                  18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty',
                                  50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninty' }
    
                self.powerNameList = ['thousand', 'lac', 'crore']
    
    
            def convertNumberToWords(self, number):
    
                # Check if there is decimal in the number. If Yes process them as paisa part.
                formString = str(number)
                if formString.find('.') != -1:
                    withoutDecimal, decimalPart = formString.split('.')
    
                    paisaPart =  str(round(float(formString), 2)).split('.')[1]
                    inPaisa = self._formulateDoubleDigitWords(paisaPart)
    
                    formString, formNumber = str(withoutDecimal), int(withoutDecimal)
                else:
                    # Process the number part without decimal separately
                    formNumber = int(number)
                    inPaisa = None
    
                if not formNumber:
                    return 'zero'
    
                self._validateNumber(formString, formNumber)
    
                inRupees = self._convertNumberToWords(formString)
    
                if inPaisa:
                    return 'Rs. %s and %s paisa' % (inRupees.title(), inPaisa.title())
                else:
                    return 'Rs. %s' % inRupees.title()
    
    
            def _validateNumber(self, formString, formNumber):
    
                assert formString.isdigit()
    
                # Developed to provide words upto 999999999
                if formNumber > 999999999 or formNumber < 0:
                    raise AssertionError('Out Of range')
    
    
            def _convertNumberToWords(self, formString):
    
                MSBs, hundredthPlace, teens = self._getGroupOfNumbers(formString)
    
                wordsList = self._convertGroupsToWords(MSBs, hundredthPlace, teens)
    
                return ' '.join(wordsList)
    
    
            def _getGroupOfNumbers(self, formString):
    
                hundredthPlace, teens = formString[-3:-2], formString[-2:]
    
                msbUnformattedList = list(formString[:-3])
    
                #---------------------------------------------------------------------#
    
                MSBs = []
                tempstr = ''
                for num in msbUnformattedList[::-1]:
                    tempstr = '%s%s' % (num, tempstr)
                    if len(tempstr) == 2:
                        MSBs.insert(0, tempstr)
                        tempstr = ''
                if tempstr:
                    MSBs.insert(0, tempstr)
    
                #---------------------------------------------------------------------#
    
                return MSBs, hundredthPlace, teens
    
    
            def _convertGroupsToWords(self, MSBs, hundredthPlace, teens):
    
                wordList = []
    
                #---------------------------------------------------------------------#
                if teens:
                    teens = int(teens)
                    tensUnitsInWords = self._formulateDoubleDigitWords(teens)
                    if tensUnitsInWords:
                        wordList.insert(0, tensUnitsInWords)
    
                #---------------------------------------------------------------------#
                if hundredthPlace:
                    hundredthPlace = int(hundredthPlace)
                    if not hundredthPlace:
                        # Might be zero. Ignore.
                        pass
                    else:
                        hundredsInWords = '%s hundred' % self.wordsDict[hundredthPlace]
                        wordList.insert(0, hundredsInWords)
    
                #---------------------------------------------------------------------#
                if MSBs:
                    MSBs.reverse()
    
                    for idx, item in enumerate(MSBs):
                        inWords = self._formulateDoubleDigitWords(int(item))
                        if inWords:
                            inWordsWithDenomination = '%s %s' % (inWords, self.powerNameList[idx])
                            wordList.insert(0, inWordsWithDenomination)
    
                #---------------------------------------------------------------------#
                return wordList
    
    
            def _formulateDoubleDigitWords(self, doubleDigit):
    
                if not int(doubleDigit):
                    # Might be zero. Ignore.
                    return None
                elif self.wordsDict.has_key(int(doubleDigit)):
                    # Global dict has the key for this number
                    tensInWords = self.wordsDict[int(doubleDigit)]
                    return tensInWords
                else:
                    doubleDigitStr = str(doubleDigit)
                    tens, units = int(doubleDigitStr[0])*10, int(doubleDigitStr[1])
                    tensUnitsInWords = '%s %s' % (self.wordsDict[tens], self.wordsDict[units])
                    return tensUnitsInWords
    
    
    if __name__ == '__main__':
    
        wGenerator = Number2Words()
        print wGenerator.convertNumberToWords(100000)
    
    类号2字(对象):
    定义初始化(自):
    ''使用有用的数据初始化类''
    self.wordsDict={1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',,
    8:'八',9:'九',10:'十',11:'十一',12:'十二',13:'十三',,
    14:'十四',15:'十五',16:'十六',17:'十七',,
    18:'十八',19:'十九',20:'二十',30:'三十',40:'四十',,
    50:'50',60:'60',70:'70',80:'80',90:'ninty'}
    self.powerNameList=['000','lac','crore']
    def Converter NumberToWords(自身,数字):
    #检查数字中是否有小数。如果是,则将其作为paisa零件进行处理。
    formString=str(数字)
    如果formString.find('.')!=-1:
    不带小数点,decimalPart=formString.split(“.”)
    paispart=str(round(float(formString),2)).split('.')[1]
    inPaisa=自我。\公式化双数字词(paisaPart)
    formString,formNumber=str(不带小数),int(不带小数)
    其他:
    #单独处理不带小数的数字部分
    formNumber=int(数字)
    印度=无
    如果不是formNumber:
    返回“零”
    self._validateEnumber(formString,formNumber)
    inRupees=self.\u转换器numbertowords(formString)
    如果在巴基斯坦:
    返回“Rs.%s和%s paisa%”(inRupees.title(),inPaisa.title())
    其他:
    返回“%s.%s”%inRupees.title()
    def_validateEnumber(self、formString、formNumber):
    断言formString.isdigit()
    #开发用于提供高达9999999的单词
    如果formNumber>9999999或formNumber<0:
    引发断言错误('超出范围')
    def_convertNumberToWords(self,formString):
    MSB,百位数,青少年=自我。\u GetGroupOfNumber(formString)
    wordsList=self.\u convertGroupsToWords(MSB、百位、青少年)
    返回“”。加入(wordsList)
    def_getGroupOfNumbers(self,formString):
    百分位,青少年=formString[-3:-2],formString[-2:]
    MSBUnformatedList=列表(formString[:-3])
    #---------------------------------------------------------------------#
    MSBs=[]
    tempstr=''
    对于MSBUnformatedList[:-1]中的num:
    tempstr=“%s%s%”(num,tempstr)
    如果len(tempstr)==2:
    MSBs.insert(0,tempstr)
    tempstr=''
    如果是tempstr:
    MSBs.insert(0,tempstr)
    #---------------------------------------------------------------------#
    返回MSB,百位,青少年
    定义(self,msb,百位,青少年):
    单词表=[]
    #---------------------------------------------------------------------#
    如果是青少年:
    青少年=智力(青少年)
    tensUnitsInWords=self.\u公式化双数字词(青少年)
    如有疑问,请填写:
    wordList.insert(0,tensUnitsInWords)
    #---------------------------------------------------------------------#
    如果是百分位:
    百分位=整数(百分位)
    如果不是百分位:
    #可能是零。忽略。
    通过
    其他:
    hundredsInWords='%s百'%self.wordsDict[hundredthPlace]
    wordList.insert(0,百字)
    #---------------------------------------------------------------------#
    如果MSB:
    MSBs.reverse()
    对于idx,枚举中的项(MSBs):
    inWords=self.\u公式化双数字词(int(项目))
    如果换言之:
    InWordsWithDemination='%s%s'(inWords,self.powerNameList[idx])
    wordList.insert(0,InWordsWithNomination)
    #---------------------------------------------------------------------#
    返回字表
    def_公式化双数字字(自身,双位数):
    如果不是整数(两位数):
    #可能是零。忽略。
    一无所获
    elif self.wordsDict.has_键(整数(双位数)):
    #全局dict具有此号码的密钥
    tensInWords=self.wordsDict[int(两位数)]
    返回时态词
    其他:
    双位数tr=str(双位数)
    十位数,单位=int(双位数tr[0])*10,int(双位数tr[1])
    tensUnitsInWords='%s%s'(self.wordsDict[tens],self.wordsDict[units])
    返回时态单位sinwords
    如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
    wGenerator=Number2Words()
    打印wGenerator.convertNumberToWords(100000)
    
    <
    from num2words import num2words
    num2words(110543, to='cardinal', lang='en_IN')
    
    'one lakh, ten thousand, five hundred and forty-three'