Python银行金额大写汉字转换

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('整')

一位南同学!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南蓬幽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值