python人民幣小寫轉大寫輔助工具
本文實例為大家分享了python人民幣大小寫轉換的具體代碼,供大家參考,具體內容如下
大家應該都知道,銀行打印賬單有時候會跟上人民幣的阿拉伯數(shù)字以及人民幣漢字大寫寫法,轉換的過程中有一定的邏輯難度,較為麻煩,所以筆者心血來潮,花了點時間簡單實現(xiàn)了一下這一轉換過程,以供初學者參考。
輸入樣例:
123.22
輸出樣例:
壹佰貳拾叁圓貳角貳分
參考代碼:
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
import sys
import re
import base64
import time
import os
import getpass
reload(sys)
sys.setdefaultencoding("utf-8")
char_arr_01 = [u"零".decode("utf8"), u"壹".decode("utf8"), u"貳".decode("utf8"), u"叁".decode("utf8"), u"肆".decode(
"utf8"), u"伍".decode("utf8"), u"陸".decode("utf8"), u"柒".decode("utf8"), u"捌".decode("utf8"), u"玖".decode("utf8")];
char_arr_02 = [u"圓".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode("utf8"), u"萬".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode(
"utf8"), u"億".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode("utf8"), u"萬".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8")]
char_arr_03 = [u"分".decode("utf8"), u"角".decode("utf8")]
def calcRMB():
sum_arr = []
in_str_dot = ""
in_str_Big = ""
flag = 0
dot_count = 0
in_str = raw_input("Please input number : ")
for i in in_str:
if i == '.':
dot_count += 1
elif ord(i) <= ord('z') and ord(i) >= ord('A'):
print("Error")
return
if len(in_str) > 12 or dot_count > 1:
print("Error")
return
in_str = unicode(in_str).decode("utf8")
out_str = ""
if in_str.find('.') != -1:
flag = 1
sum_arr = in_str.split('.')
in_str_Big = sum_arr[0]
if flag==1:
in_str_dot = sum_arr[1]
for i in range(len(in_str_Big)):
if cmp(in_str_Big[i],'0') == 0 and (len(in_str_Big)-1-i)%4 != 0:
out_str = out_str + char_arr_01[ord(in_str_Big[i])-ord('0')]
else:
out_str = out_str + char_arr_01[ord(in_str_Big[i])-ord('0')]
out_str = out_str + char_arr_02[len(in_str_Big)-1-i]
while out_str.find(u"零零".decode("utf8")) != -1:
out_str = out_str.replace(u"零零".decode("utf8"), u"零".decode("utf8"))
out_str = out_str.replace(u"零億".decode("utf8"),u"億".decode("utf8"))
out_str = out_str.replace(u"零萬".decode("utf8"),u"萬".decode("utf8"))
if out_str != u"零元".decode("utf8"):
out_str = out_str.replace(u"零圓".decode("utf8"),u"圓".decode("utf8"))
if len(in_str_dot) > 2 and flag == 1:
print("False !!")
return
if flag == 1:
for i in range(len(in_str_dot)):
out_str = out_str + char_arr_01[ord(in_str_dot[i])-ord('0')]
out_str = out_str + char_arr_03[len(in_str_dot)-1-i]
print(out_str)
def main():
while 1:
os.system("cls")
calcRMB()
print()
end_flag = raw_input("Try Again ? (y/n)")
if end_flag == 'y' or end_flag == 'Y':
continue
elif end_flag == 'n' or end_flag == 'N':
break
else:
print("\nError!!")
break
if __name__ == '__main__':
main()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Anaconda安裝pytorch及配置PyCharm 2021環(huán)境
小編使用的是python3.8版本,為了防止訪問量過大導致http連接失敗,所以采用本地安裝,具體安裝方法本文給大家詳細介紹,在文章底部給大家提到了PyCharm 2021配置環(huán)境的方法,感興趣的朋友一起看看吧2021-06-06
python GUI庫圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細使用方法
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細使用方法,需要的朋友可以參考下2020-02-02
Python遠程開發(fā)環(huán)境部署與調試過程圖解
這篇文章主要介紹了Python遠程開發(fā)環(huán)境部署與調試過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
教你用Python+selenium搭建自動化測試環(huán)境
今天給大家?guī)淼氖顷P于Python的相關知識,文章圍繞著如何用Python+selenium搭建自動化測試環(huán)境展開,文中有非常詳細的介紹,需要的朋友可以參考下2021-06-06
python 實現(xiàn)將小圖片放到另一個較大的白色或黑色背景圖片中
今天小編就為大家分享一篇python 實現(xiàn)將小圖片放到另一個較大的白色或黑色背景圖片中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

