问题描述:
银行在打印票据的时候,常常需要将阿拉伯数字表示的人民币金额转换为大写表示,现在请你来完成这样一个程序。
在中文大写方式中,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保留。

1 num_dic = {'0':u'零', '1':u'壹', '2':u'贰', '3':u'叁', '4':u'肆', '5':u'伍', '6':u'陆', '7':u'柒', '8':u'捌', '9':u'玖'} 2 unit_dic = {0:u'仟', 1:'佰', 2:'拾', 3:'万', 4:'仟', 5:'佰', 6:'拾', 7:''} 3 decimal_dic={0:u'角',1:u'分',2:u'厘',3:u'毫'} 4 def convert_int(num): 5 temp = [] 6 t = str('%8d'%abs(num)) #重点:把数字换成固定长度8位的字符串 7 for s in range(0,4): 8 if t[s] != ' ' and t[s] != '0': 9 temp.append(num_dic[t[s]]) 10 temp.append(unit_dic[s]) 11 if t[s] == '0' and s <3 and t[s+1] != '0': 12 temp.append(num_dic[t[s]]) 13 if(t[2] != ' ' and t[3] == '0'): 14 temp.append('万') 15 for s in range(4,8): 16 if t[s] != ' ' and t[s] != '0': 17 temp.append(num_dic[t[s]]) 18 temp.append(unit_dic[s]) 19 if t[s] == '0' and s < 7 and t[s+1] != '0': 20 temp.append(num_dic[t[s]]) 21 temp = ''.join(temp) 22 if num < 0: 23 temp = '负' + temp 24 if num == 0: 25 temp = '零' + temp 26 27 return temp + u'圆' 28 29 def convert_decimal(num): 30 t = str('%4d'%num) 31 temp = [] 32 for s in range(0,4): 33 if t[s] != ' ' and t[s] != '0': 34 temp.append(num_dic[t[s]]) 35 temp.append(decimal_dic[s]) 36 if t[s] == '0' and s < 3 and t[s + 1] != '0': 37 temp.append(num_dic[t[s]]) 38 temp = ''.join(temp) 39 if len(temp) == 0: 40 return '整' 41 return temp 42 43 def convert_money(money): 44 num = round(float(money),4) 45 integer,decimal = str(num).split('.') 46 result_int = convert_int(int(integer)) 47 result_decimal = convert_decimal(int(decimal)) 48 return result_int+result_decimal 49 50 money = input('请输入转换的金额:') 51 print(convert_money(money))