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> / ...
随机推荐
- vue数据渲染、条件判断及列表循环
1.数据渲染 {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...
- 前端每日实战:94# 视频演示如何用纯 CSS 创作一台拍立得照相机
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YjYgey 可交互视频 此视频是可 ...
- python中实现查找字符串的find函数
第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2为要查找的字符串 def index_of_s ...
- error C2872: ‘ofstream’ : ambiguous symbol
转自VC错误:http://www.vcerror.com/?p=1123 问题描述: 编译时出现: error C2872: 'ofstream' : ambiguous symbol error ...
- LINUX shell脚本相关
调试脚本 测试脚本语法:bash -n file.sh 查看脚本每一步执行情况:bash -x file.sh 位置变量:$1,$2,... 特殊变量: %?:最后一个命令的执 ...
- jenkins构建后操作archive the artfacts的用法
参考: https://blog.csdn.net/liqiangeastsun/article/details/79062806 Jenkins构建完成存档 Archive the artifact ...
- js另存为、打印、属性、加入收藏、关闭等代码
js打开代码 <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开> &l ...
- CSS实现文字阴影的效果
CSS中有两种阴影效果,一种是DropShadow(投影),另一种是Shadow(阴影).1.DropShadow语法:{FILTER:DropShadow(Color=color,OffX=offX ...
- cdh5.7 做完HA后hive 查询出现异常: expected: hdfs://nameservice
异常信息如下: select * from b_pt_pr_customer_address_info limit 19; FAILED: SemanticException Unable to de ...
- JavaScript 标准参考教程(alpha) 阮一峰
JavaScript 标准参考教程(alpha)http://javascript.ruanyifeng.com/#introduction