python大写中文转阿拉伯数字

#!/usr/bin/env python

-- coding:utf-8 --

‘’’
@author: autobaba
@software: Pycharm
@file: word2number.py
@time: 20
@desc:
‘’’

支持万亿以内的转换

import re

def word2number(amount):
chinese_num = {‘零’: 0, ‘壹’: 1, ‘贰’: 2, ‘叁’: 3, ‘肆’: 4, ‘伍’: 5, ‘陆’: 6, ‘柒’: 7, ‘捌’: 8, ‘玖’: 9}
chinese_amount = {‘分’: 0.01, ‘角’: 0.1, ‘元’: 1, ‘拾’: 10, ‘佰’: 100, ‘仟’: 1000, ‘圆’: 1}
amount_float = 0
if ‘亿’ in amount:
yi = re.match(r’(.+)亿.‘, amount).group(1)
amount_yi = 0
for i in chinese_amount:
if i in yi:
amount_yi += chinese_num[yi[yi.index(i) - 1]] * chinese_amount[i]
if yi[-1] in chinese_num.keys():
amount_yi += chinese_num[yi[-1]]
amount_float += amount_yi * 100000000
amount = re.sub(r’.+亿’, ‘’, amount, count=1)
if ‘万’ in amount:
wan = re.match(r’(.+)万.
‘, amount).group(1)
amount_wan = 0
for i in chinese_amount:
if i in wan:
amount_wan += chinese_num[wan[wan.index(i) - 1]] * chinese_amount[i]
if wan[-1] in chinese_num.keys():
amount_wan += chinese_num[wan[-1]]
amount_float += amount_wan * 10000
amount = re.sub(r’.+万’, ‘’, amount, count=1)

amount_yuan = 0
for i in chinese_amount:
    if i in amount:
        if amount[amount.index(i) - 1] in chinese_num.keys():
            amount_yuan += chinese_num[amount[amount.index(i) - 1]] * chinese_amount[i]
amount_float += amount_yuan

return amount_float

print(word2number(“伍拾万伍仟柒佰玖拾叁元整”))

print(word2number(“壹佰柒拾万玖仟叁佰零伍元玖角伍分”))

print(word2number(“壹佰零柒元整”))

print(word2number(“壹仟零柒元整”))

chinese_num = {‘零’: 0, ‘壹’: 1, ‘贰’: 2, ‘叁’: 3, ‘肆’: 4, ‘伍’: 5, ‘陆’: 6, ‘柒’: 7, ‘捌’: 8, ‘玖’: 9}

chinese_amount = {‘分’: 0.01, ‘角’: 0.1, ‘元’: 1, ‘拾’: 10, ‘佰’: 100, ‘仟’: 1000, ‘圆’: 1}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值