# -*- coding: utf-8 -*- # 最大值:九兆九千九百九十九億九千九百九十九萬九千九百九十九 import re p = ['', '十', '百', '千', '萬', '十', '百', '千', '億', '十', '百', '千', '兆'] s = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'] def num2zh(num_str): res = '' t = list(str(num_str)) t.reverse() for idx, tmp in enumerate(t): if int(tmp) == 0: res = '零' + res if idx % 4 == 0 and idx > 0: res = p[idx] + res continue res = s[int(tmp)] + p[idx] + res # 零+ -> 零 out = re.sub(r'(\xe9\x9b\xb6)+', '零', res) # 零萬 -> 萬 out = re.sub(r'\xe9\x9b\xb6\xe4\xb8\x87', '萬', out) # 一十 -> 十 out = re.sub(r'(^\xe4\xb8\x80\xe5\x8d\x81)', '十', out) # 零億 -> 億 out = re.sub(r'\xe9\x9b\xb6\xe4\xba\xbf', '億', out) # 億萬 -> 億 out = re.sub(r'\xe4\xba\xbf\xe4\xb8\x87', '億', out) # 兆億 -> 兆 out = re.sub(r'\xe5\x85\x86\xe4\xba\xbf', '兆', out) # 去掉最後的零 out = re.sub(r'(\xe9\x9b\xb6)$', '', out) return out print(num2zh(1234))