Python實現阿拉伯數字轉漢語數字(范圍:億)


環境:win10+Python3  實現:億范圍內正整數阿拉伯數字轉漢語數字(未做健壯性判斷)

 1 import re
 2 
 3 
 4 num_dict = {'1':'', '2':'', '3':'', '4':'', '5':'', '6':'', '7':'', '8':'', '9':'', '0':'', }
 5 index_dict = {1:'', 2:'', 3:'', 4:'', 5:'', 6:'', 7:'', 8:'', 9:''}
 6 while True:
 7     num = input(">>")
 8 
 9     nums = list(num)
10     nums_index = [x for x in range(1, len(nums)+1)][-1::-1]
11 
12     str = ''
13     for index, item in enumerate(nums):
14         str = "".join((str, num_dict[item], index_dict[nums_index[index]]))
15 
16     str = re.sub("零[十百千零]*", "", str)
17     str = re.sub("零萬", "", str)
18     str = re.sub("億萬", "億零", str)
19     str = re.sub("零零", "", str)
20     str = re.sub("零\\b" , "", str)
21     print(str)

運行示例:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM