python人民币小写转大写辅助工具
作者:雨者 时间:2022-02-10 15:28:19
本文实例为大家分享了python人民币大小写转换的具体代码,供大家参考,具体内容如下
大家应该都知道,银行打印账单有时候会跟上人民币的阿拉伯数字以及人民币汉字大写写法,转换的过程中有一定的逻辑难度,较为麻烦,所以笔者心血来潮,花了点时间简单实现了一下这一转换过程,以供初学者参考。
输入样例:
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()
来源:https://blog.csdn.net/peterz1997/article/details/78149082
标签:python,人民币,小写,大写
0
投稿
猜你喜欢
python去掉行尾的换行符方法
2021-10-24 02:36:22
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
2022-04-22 00:23:52
如何随机显示图片计数器?
2010-05-16 15:21:00
为自己的网站添加RSS功能
2007-11-05 19:18:00
opencv python统计及绘制直方图的方法
2021-12-27 21:52:45
Django rest framework分页接口实现原理解析
2023-08-22 21:26:36
利用python写api接口实战指南
2021-03-29 06:07:18
Python初识二叉树续之实战binarytree
2021-04-05 22:22:51
mysql错误处理之ERROR 1665 (HY000)
2024-01-21 12:16:54
mysql 5.7如何安装 mysql 5.7安装配置教程
2024-01-18 06:18:39
Python3 适合初学者学习的银行账户登录系统实例
2021-06-16 05:09:22
vue3+vite使用jsx和tsx详情
2024-05-10 14:15:47
mysql unix准换时间格式查找指定日期数据代码
2024-01-16 00:35:24
Pytorch中的图像增广transforms类和预处理方法
2022-09-10 22:21:06
详解Go语言中单链表的使用
2024-02-01 11:55:03
Mysql Workbench查询mysql数据库方法
2024-01-27 19:18:54
Golang学习笔记(四):array、slice、map
2024-04-30 10:02:32
CentOS+Nginx+PHP+MySQL详细配置(图解)
2023-11-24 03:04:13
python访问sqlserver示例
2022-07-15 07:25:01
Python3多进程 multiprocessing 模块实例详解
2022-05-27 11:11:51