python 金额数字 转大写 --------学习记录

附代码

def moneytobig(num):
    if num >= 999999999999.99:
        return '数值过大超出范围'

    dict1 = {1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍',
             6: '陆', 7: '柒', 8: '捌', 9: '玖', 0: '零'}
    dict2 = {1: '元', 2: '拾', 3: '佰', 4: '仟', 5: '万',
             6: '拾', 7: '佰', 8: '仟', 9: '亿', 10: '拾',
             11: '佰', 12: '仟', 13: '角', 14: '分', 15: '整'}

    money = ''  # 定义大写参数
    flag = False  # 去除多余的十百千
    flag2 = False  # 增加零
    count = 0
    count2 = 12
    str_num = str(num)
    money_split = str_num.split('.')  # 根据小数点切割金额
    money_one = list(str(money_split[:1])[2:-2])
    money_two = list(str(money_split[1:])[2:-2])
    if len(money_two) <= 1:
        if_int = True
    else:
        if_int = False
    # 整数处理
    for i in reversed(money_one):
        count = count + 1
        if int(i) == 0:
            if flag is True:
                if ((count - 1) % 4) != 0:
                    continue
                else:
                    money = dict2[count] + money
            else:
                if flag2 is False:
                    money = dict2[count] + money
                else:
                    if ((count - 1) % 4) != 0:
                        money = '零' + money
                    else:
                        money = dict2[count] + '零' + money
                flag = True
        else:
            flag = False
            flag2 = True
            money = dict1[int(i)] + dict2[count] + money

    # 小数部分
    for j in money_two:
        count2 = count2 + 1
        if int(j) == 0 and count2 == 13:
            money = money + '零'
        else:
            money = money + dict1[int(j)] + dict2[count2]
    if if_int is True:
        money = money + dict2[15]
    return money


moneybig = moneytobig(1111111111)
print(moneybig)

函数功能说明
最大支持999999999999.99 也就是不超过一万亿。

参考链接

新手小白,如有错误,请指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值