Python小程序 -- 人民币小写转大写辅助工具
2023-09-11 14:14:05 时间
大家应该都知道,银行打印账单有时候会跟上人民币的阿拉伯数字以及人民币汉字大写写法,转换的过程中有一定的逻辑难度,较为麻烦,所以笔者心血来潮,花了点时间简单实现了一下这一转换过程,以供初学者参考。
输入样例:
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语法基础_01.Python基础
- what does bad color sequence mean in python turtle?
- python 保存图片_用Python从视频中提取每一帧的图片
- python是否存在某个字符串 效率最高_Python中判断子串存在的性能比较及分析总结...
- Python 序列、列表(List)、元组(Tuple)
- Python 读写CSV文件实例
- Python url编码、url解码
- python文件的编译
- Python初学者如何系统的学习python————Python入门学习指南--内附学习路径
- 24岁非计算机专业工科妹子裸辞转行Python程序员,自学Python三个月,零基础如何系统学习python,从入门到精通?
- Python分享:python爬虫可以用来做什么?
- 4年功能测试,我一进阶python接口自动化测试,跳槽拿了20k......
- 【python】+704个常用工具Python库
- 基于STM32+Python+MySQL实现在线温度计设计和制作【100010362】
- 《Python编程实战:运用设计模式、并发和程序库创建高质量程序》—— 2.5 外观模式
- Python unittest有哪些使用方法
- Python 教程大全之使用 python 获取唯一单词的计数
- Python 实现被动收入教程之我如何使用 python 制作我的第一个高级telegram机器人
- Jenkins持续集成实战之Jenkins构建Python项目提示:'python' 不是内部或外部命令,也不是可运行的程序。
- 《从问题到程序:用Python学编程和计算》——1.3 程序开发
- 《从问题到程序:用Python学编程和计算》——3.2 递归
- 【Python分布式服务框架】python实现gRPC服务
- 【Python基础】我的第一个 python 程序
- 数据预处理技术与对应python代码实现
- Unity 工具 之 报错 Jenkins 执行/调用 Python 脚本,报错提示 ‘python‘ 不是内部或外部命令,也不是可运行的程序或批处理文件
- Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的词汇多样性分析
- Python比较运算符
- 【Python】对一个字符串中的所有英文字母进行排序
- [Python] split()方法