大家應該都知道,銀行列印賬單有時候會跟上人民幣的阿拉伯數字以及人民幣漢字大寫寫法,轉換的過程中有一定的邏輯難度,較為麻煩,所以筆者心血來潮,花了點時間簡單實現了一下這一轉換過程,以供初學者參考。
輸入樣例:
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"肆".fjwuzswdecode(
"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"仟".dec程式設計客棧ode(
"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.fiwww.cppcns.comnd('.') != -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 != 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()
本文標題: python人民幣小寫轉大寫輔助工具
本文位址:
人民幣小寫轉大寫
package day00 public class moneyelse result result.replaceall 零角 零 result result.replaceall 零分 零 result result.replaceall 角零 角 if result.equals 零零 ret...
人民幣小寫轉大寫
c code 例如 new money 200 tostring 貳佰元 namespace skyiv.util catch system.console.writeline 大寫 newmoney m 該類過載的 tostring 方法返回的是大寫金額字串 class money 建構函式 pu...
人民幣小寫轉大寫
小寫金額轉成大寫。param input return public static string numtochinese string input catch exception e int len 0 if temp.indexof 1 len temp.length else len temp...