python实现人民币大写转换
问题描述:
银行在打印票据的时候,常常需要将阿拉伯数字表示的人民币金额转换为大写表示,现在请你来完成这样一个程序。
在中文大写方式中,0到10以及100、1000、10000被依次表示为: 零 壹 贰 叁 肆 伍 陆 柒 捌 玖 拾 佰 仟 万
以下的例子示范了阿拉伯数字到人民币大写的转换规则:
1 壹圆
11 壹拾壹圆
111 壹佰壹拾壹圆
101 壹佰零壹圆
-1000 负壹仟圆
1234567 壹佰贰拾叁万肆仟伍佰陆拾柒圆
现在给你一个整数a(|a|<100000000), 请你打印出人民币大写表示.
例如:a=1
则输出:壹圆
注意:请以Unicode的形式输出答案。提示:所有的中文字符,在代码中直接使用其Unicode的形式即可满足要求,中文的Unicode编码可以通过如下方式获得:u'壹'。
这题目最头疼的地方是当一串数字中间和末尾都存在0的时候,要去掉重复出现的0和对应的单位。
例如: 6100 0310元
对应的大写为: 陆仟壹佰万零叁佰壹拾圆
这里6100后面的零转换成大写之后都去掉了,0310个位数上的0也去掉了,但是千位上的数字0保留。
num_dic = {'':u'零', '':u'壹', '':u'贰', '':u'叁', '':u'肆', '':u'伍', '':u'陆', '':u'柒', '':u'捌', '':u'玖'}
unit_dic = {0:u'仟', 1:'佰', 2:'拾', 3:'万', 4:'仟', 5:'佰', 6:'拾', 7:''}
decimal_dic={0:u'角',1:u'分',2:u'厘',3:u'毫'}
def convert_int(num):
temp = []
t = str('%8d'%abs(num)) #重点:把数字换成固定长度8位的字符串
for s in range(0,4):
if t[s] != ' ' and t[s] != '':
temp.append(num_dic[t[s]])
temp.append(unit_dic[s])
if t[s] == '' and s <3 and t[s+1] != '':
temp.append(num_dic[t[s]])
if(t[2] != ' ' and t[3] == ''):
temp.append('万')
for s in range(4,8):
if t[s] != ' ' and t[s] != '':
temp.append(num_dic[t[s]])
temp.append(unit_dic[s])
if t[s] == '' and s < 7 and t[s+1] != '':
temp.append(num_dic[t[s]])
temp = ''.join(temp)
if num < 0:
temp = '负' + temp
if num == 0:
temp = '零' + temp
return temp + u'圆'
def convert_decimal(num):
t = str('%4d'%num)
temp = []
for s in range(0,4):
if t[s] != ' ' and t[s] != '':
temp.append(num_dic[t[s]])
temp.append(decimal_dic[s])
if t[s] == '' and s < 3 and t[s + 1] != '':
temp.append(num_dic[t[s]])
temp = ''.join(temp)
if len(temp) == 0:
return '整'
return temp
def convert_money(money):
num = round(float(money),4)
integer,decimal = str(num).split('.')
result_int = convert_int(int(integer))
result_decimal = convert_decimal(int(decimal))
return result_int+result_decimal
money = input('请输入转换的金额:')
print(convert_money(money))
python实现人民币大写转换
python实现人民币大写转换的更多相关文章
- FastReport调用Delphi中的人民币大写转换自定义函数
FastReport调用Delphi中的人民币大写转换自定义函数 FastReport调用Delphi中的人民币大写转换自定义函数 function TJzpzEdit1.MoneyCn(mmje ...
- 【办公-Word-VB】人民币大写转换-带完整注释
完整代码见:我的CSDN博客 -------------------- 应公司财务人员的请求,需在Word中做个:输入阿拉伯数字,自动转换成大写,并填充到Word控件中对应的亿.万.千控件格子的功能, ...
- java递归算法实现 数字人民币大写转换
最近穷死了 ,没钱吃饭ing 写点钱给自己吧!public class Test{ public static String getChar(long a){ int b = (int)a; Map ...
- Java实现人民币大写精讲
想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...
- Java实现人民币大写代码解析
想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...
- ORACLE数字转换人民币大写
ORACLE 数字转换人民币大写 示例. 数字 :183066999230.68 人民币大写 :壹仟捌佰参拾亿陆仟陆佰玖拾玖万玖仟贰佰参 ...
- js 将数字转换成人民币大写的方法
//将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...
- 人民币大写金额转换C#方法
方法的代码如下: /// <summary> /// 人民币大写 /// </summary> /// <param name="input"> ...
- NET 人民币大写
/***** HongShijin** Me@HongShijin.com** 2009-3-15 10:13:00.00000** text/C#***/ /// <summary> / ...
随机推荐
- (四)添加yaffs2文件系统支持
1. 获取yaffs2源码 在linux工作目录下进行clone操作: git clone git://www.aleph1.co.uk/yaffs2 完成后会在当前目录下产生yaffs2的源码目录: ...
- 日期和时间格式(ISO 8601)
参考 ISO 8601 - Wikipedia ISO 8601 Date and time format
- feignClient传参(参数为对象类型)的一个坑
客户端 @RequestMapping(value = "/friendCircleComment/comment",method = RequestMethod.POST) R ...
- Problem accessing /jenkins/. Reason:
这是一个Jenkins的Bug.临时解决方法是:在浏览器中手工输入:http://<ip>:<port>.不要访问"/jenkins"这个路径.
- Java学习之线程通信(多线程(synchronized))--生产者消费者
分析线程经典案例生产者消费者 /** 共享数据 */ class Resource { private String name; private int count=1; private boolea ...
- mongo 数据库存储
mongo 数据库,获取有赞的数据. from app import mongo from app.external.yz.goods_api import YzGoodsApi from openp ...
- rf, xgboost和GBDT对比;xgboost和lightGbm
1. RF 随机森林基于Bagging的策略是Bagging的扩展变体,概括RF包括四个部分:1.随机选择样本(放回抽样):2.随机选择特征(相比普通通bagging多了特征采样):3.构建决策树:4 ...
- 配置访问公网主机上的jupyter notebook
文章结构: 一.安装python 二.安装并配置jupyter并配置jupyter 三.第一个python程序 一.安装python 1.1下载python安装包 # wget https://www ...
- maxim - Android UI压力测试
项目介绍 项目地址:https://github.com/zhangzhao4444/Maxim 与monkey对比优势: 快 稳:只进行有意义的操作,防误点状态栏,不会乱断网.卸载应用 支持脱机运行 ...
- Python之计算当前月份的日期范围(calendar、datetime)
在当前月份中循环每一天大致的思路就是先计算出当月的第一天和下个月的第一天日期,还有当月总共有多少天,然后把第一天日期按照月总天数累加到下个月的第一天,就ok 啦 from datetime impor ...