Python银行金额大写汉字转换
业务需求:
银行电子支票业务在金额部分需要使用大写的汉字,因此需要将用户录入的数字信息转变为汉字。
• 目前只需完成1~5位整数转换即可。
示例:
输入金额:> 32542
汉字转换:> 叁 萬 贰 仟 伍 佰 肆 拾 贰 圆 整
关键技术分析:
• 使用For循环完成数字每一位的拆解。
• 利用列表下标实现对位转换。
编程思路:
程序可以拆分为3个环节实现:
需要创建两个列表,为后续对位转换做准备:
环节1:计算出用户输入金额的位数;
环节2:利用已知位数完成每一位的拆解;
环节3:通过列表下标对位实现最终输出。
• 开发技巧:
需要创建两个列表,为后续对位转换做准备:
• 汉字列表:[‘零’, ‘壹’, ‘贰’, ‘叁’, ‘肆’, ‘伍’, ‘陆’, ‘柒’, ‘捌’, ‘玖’, ‘拾’]
• 单位列表:[‘圆’,‘拾’, ‘佰’, ‘仟’, ‘萬’]
list_chinese = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾']
list_unit = ['圆', '拾', '佰', '仟', '萬']
money = input('input 金额 五位以下: ')
price = int(money[:5]) # 去除首位的0
list_price = list(str(price))
end_zero = 1 # 末尾是否为0
now = 1 # 当前是否为0
len_price = len(list_price)
for i in range(len_price):
list_price[i] = list_chinese[int(list_price[i])] # 对位转换成大写
zero = list_chinese[0] # 零
if list_price[-1] == zero:
end_zero = 0
for i in range(len_price):
if len_price == 1 and end_zero == 0:
print(list_price[0], end='')
print(list_unit[0], end='') # 0时
break
elif i == len_price - 1 and end_zero == 0:
print(list_unit[0], end='')
break
elif i == len_price - 1 and end_zero == 1:
print(list_price[i], end='')
print(list_unit[len_price - i - 1], end='')
else:
if list_price[i] == zero:
now = 0 # 当前为0
else:
now = 1
if now == 1 or (now == 0 and list_price[i - 1] != zero and end_zero == 1):
print(list_price[i], end='')
if now == 1 and i != len_price - 1: # 若当前不为0
print(list_unit[len_price - i - 1], end='')
print('整')
一位南同学!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/147523.html