Python编写数字转换成中文大写

问题描述:输入一串数字金额,然后打印出它的中文大写。

利用列表下标进行转换。

list1 = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾']
list2 = ['圆', '拾', '佰', '仟', '萬']

money = str(int(input("请输入金额:")))  # 预防输入0开头的数字
money2 = ''
for i in range(0, len(money)):
    if int(money[i]) != 0:
        money2 += list1[int(money[i])] + list2[len(money) - i - 1]
    else:
        if money2[-1] != "零":
            money2 += "零"
if money2[-1] == "零":
    print(money2[0:len(money2) - 1] + "圆整")
else:
    print(money2 + "整")

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是Python代码实现: ```python # encoding:utf-8 def transition(number): if not isinstance(number, (int, float)): return '参数类型错误' if number > 999999999999.99 or number < 0: return '金额范围有误' unit_list = ['千', '百', '十', ''] chinese_num_list = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] chinese_unit_list = ['', '万', '亿', '兆'] integral, decimal = str(number).split('.') integral_list = list(integral) integral_list.reverse() unit_index, zero_num, chinese_num, chinese_money = 0, 0, 0, '' for i, num in enumerate(integral_list): if num == '0': zero_num += 1 if unit_index % 4 == 0: chinese_money = chinese_unit_list[unit_index // 4] + chinese_money if unit_index % 4 == 3: chinese_money = '零' + chinese_money else: if zero_num > 0: chinese_money = chinese_num_list[0] + chinese_money zero_num = 0 chinese_num = chinese_num_list[int(num)] chinese_money = chinese_num + unit_list[unit_index % 4] + chinese_money if unit_index % 4 == 3: chinese_money = chinese_unit_list[unit_index // 4] + chinese_money unit_index += 1 if zero_num > 0: chinese_money = chinese_num_list[0] + chinese_money chinese_money += '元' if decimal == '00': chinese_money += '整' else: chinese_money += chinese_num_list[int(decimal[0])] + '角' if decimal[1] != '0': chinese_money += chinese_num_list[int(decimal[1])] + '分' return chinese_money if __name__ == '__main__': number = 123456789.33 result = transition(number) print(result) ``` 这个函数可以将数字金额转换为中文大写金额,例如将“123456789.33”转换为“壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元叁角叁分”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值