日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打印支票_从数字转换为单词以打印支票的Python脚本

發布時間:2024/1/1 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打印支票_从数字转换为单词以打印支票的Python脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

分析信息:此腳本的性能較差,執行時間為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)

總結

以上是生活随笔為你收集整理的python打印支票_从数字转换为单词以打印支票的Python脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。