问题描述:

银行在打印票据的时候,常常需要将阿拉伯数字表示的人民币金额转换为大写表示,现在请你来完成这样一个程序。

在中文大写方式中,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实现人民币大写转换的更多相关文章

  1. FastReport调用Delphi中的人民币大写转换自定义函数

    FastReport调用Delphi中的人民币大写转换自定义函数   FastReport调用Delphi中的人民币大写转换自定义函数 function TJzpzEdit1.MoneyCn(mmje ...

  2. 【办公-Word-VB】人民币大写转换-带完整注释

    完整代码见:我的CSDN博客 -------------------- 应公司财务人员的请求,需在Word中做个:输入阿拉伯数字,自动转换成大写,并填充到Word控件中对应的亿.万.千控件格子的功能, ...

  3. java递归算法实现 数字人民币大写转换

    最近穷死了 ,没钱吃饭ing 写点钱给自己吧!public class Test{ public static String getChar(long a){ int b = (int)a; Map ...

  4. Java实现人民币大写精讲

    想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...

  5. Java实现人民币大写代码解析

    想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...

  6. ORACLE数字转换人民币大写

    ORACLE 数字转换人民币大写     示例.   数字                    :183066999230.68 人民币大写        :壹仟捌佰参拾亿陆仟陆佰玖拾玖万玖仟贰佰参 ...

  7. js 将数字转换成人民币大写的方法

    //将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...

  8. 人民币大写金额转换C#方法

    方法的代码如下: /// <summary> /// 人民币大写 /// </summary> /// <param name="input"> ...

  9. NET 人民币大写

    /***** HongShijin** Me@HongShijin.com** 2009-3-15 10:13:00.00000** text/C#***/ /// <summary> / ...

随机推荐

  1. 20175213 2018-2019-2 《Java程序设计》第11周学习总结

    教材学习内容总结 URL类是java.net包中的一个重要的类,URL的实例封装着一个统一资源定位符(Uniform Resource Locator),使用URL创建对象的应用程序称作客户端程序 U ...

  2. shell 截取变量的字符串

    假设有变量 var=http://www.linuxidc.com/test.htm一 # 号截取,删除左边字符,保留右边字符.echo ${var#*//}其中 var 是变量名,# 号是运算符,* ...

  3. VB - sendKey

    Set WshShell=WScript.CreateObject("WScript.Shell") WshShell = SendKeys string “string”:表示要 ...

  4. 49-python基础-python3-列表-常用列表统计函数-max()-min()-sum()

    max() min() sum() 1-数字列表统计 实例: 2-字符串列表统计. 根据ASCII码大小统计字符串列表的min()和max(). 注意:sum()函数无法统计字符串列表. 实例:

  5. how to prevent lowmemorykiller from killing processes

    Hi there, I've upgraded a number of test systems to the latest Saucy beta. I've seen quite a few cas ...

  6. JS事件循环(Event Loop)机制

    前言 众所周知,为了与浏览器进行交互,Javascript是一门非阻塞单线程脚本语言. 为何单线程? 因为如果在DOM操作中,有两个线程一个添加节点,一个删除节点,浏览器并不知道以哪个为准,所以只能选 ...

  7. for循环(C语言型)举例

  8. docker 安装Filebeat

    1.查询镜像 docker search filebeat 2.拉取镜像 我此处选择的是prima/filebeat docker pull prima/filebeat 3.创建配置文件 fileb ...

  9. PL/SQL to update all columns

    undefine schema_name; declare l_Err ); begin for r in (select atc.table_name, atc.column_name, atc.d ...

  10. sqlldr - exit error code 2 in unix (merged)

    http://www.orafaq.com/forum/t/146582/ Thank you for your reply. Load has been successful all the tim ...