[Python] 纯文本查看 复制代码 def cn2an(chinese_num):
number_map = {
"零": 0,
"一": 1,
"二": 2,
"三": 3,
"四": 4,
"五": 5,
"六": 6,
"七": 7,
"八": 8,
"九": 9,
}
unit_map = {"十": 10, "百": 100, "千": 1000, "万": 10000, "亿": 100000000}
arabic_num = 0
unit = 1
num = 1
hundred_million_num = 0
for index, cn_num in enumerate(chinese_num):
if cn_num in number_map:
num = number_map[cn_num]
if index == len(chinese_num) - 1:
arabic_num = hundred_million_num + arabic_num + num
elif cn_num in unit_map:
unit = unit_map[cn_num]
if unit == 10000:
arabic_num = (arabic_num + num) * unit
elif unit == 100000000:
hundred_million_num = (arabic_num + num) * unit
arabic_num = 0
else:
arabic_num = arabic_num + num * unit
num = 0
if index == len(chinese_num) - 1:
arabic_num = hundred_million_num + arabic_num + num
else:
raise ValueError(f"{cn_num} 不在转化范围内")
return arabic_num
目前测试下来到亿单位的都可以正常转换 |