您的当前位置:首页正文

人民币金额大写转小写

2024-11-30 来源:个人技术集锦

将大写的金额转换成小写
对:“壹佰零壹亿叁仟零叁拾万零陆佰零陆元玖角玖分”
按”亿”,”万”,”元”进行切分
得到 [‘壹佰零壹’, ‘叁仟零叁拾’, ‘零陆佰零陆’, ‘玖角玖分’]
对list中每一个串进行转换然后乘以它的进制
最后累加

import re
money_dict={ "壹":1, "贰":2,"叁":3, "肆":4,
    "伍":5,"陆":6,"柒":7,"捌":8,"玖":9,
    "分":0.01,"角":0.1,"元":1,"拾":10,
    "佰":100,"仟":1000,"万":10000,"亿":100000000,
}
format_num = ["壹","贰","叁","肆","伍","陆","柒","捌","玖"]
format_hex = ["分","角","元","拾","佰","仟"]
format_d=["亿","万","元"]
def parse(str):
    memory = 0
    total = 0
    if len(str)==1:
        total=money_dict.get(str)
    else:
        for index, s in enumerate(str):
            num = money_dict.get(s)
            if s in format_num:
                memory = num
                if index ==len(str)-1:
                    total = total + memory
            elif s in format_hex:
                memory = memory * num
                total = total + memory
    return total

# def p_str(str):
#     x = 0
#     while str.find("零", x) != -1:
#         b = str.find("零", x)
#         x = b + 2
#         str = str[:b] + str[b + 1] + "零" + str[b + 2:]
#     return str

def p_data(str):
    list_d = re.split("[亿万元]", str)
    resu_num = 0
    for d in format_d:
        if d in str:
            list_d, resu_num = p_compute(list_d, resu_num, d)
    if list_d[0] == "整":
        return resu_num
    else:
        num = parse(list_d[0])
        resu_num = resu_num + num
        return resu_num

def p_compute(list_d,resu_num,hex):
    num = parse(list_d[0])
    resu_num = resu_num + num * money_dict.get(hex)
    list_d = list_d[1:]
    return list_d,resu_num

if __name__ == '__main__':
    str="叁拾万零玖拾元玖分"
    #str=p_str(str)
    print(str)
    print(p_data(str))
    #parse(str)

求一个简单易用的demo。。。

显示全文