本文章向大家介绍利用python怎么实现一个人民币大小写转换工具,主要包括利用python怎么实现一个人民币大小写转换工具的使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
python可以做什么
Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。
输入样例:
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()
到此这篇关于利用python怎么实现一个人民币大小写转换工具的文章就介绍到这了,更多相关的内容请搜索天达云以前的文章或继续浏览下面的相关文章希望大家以后多多支持天达云!