# -*- 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'(xe9x9bxb6)+', '零', res) # 零万 -> 万 out = re.sub(r'xe9x9bxb6xe4xb8x87', '万', out) # 一十 -> 十 out = re.sub(r'(^xe4xb8x80xe5x8dx81)', '十', out) # 零亿 -> 亿 out = re.sub(r'xe9x9bxb6xe4xbaxbf', '亿', out) # 亿万 -> 亿 out = re.sub(r'xe4xbaxbfxe4xb8x87', '亿', out) # 兆亿 -> 兆 out = re.sub(r'xe5x85x86xe4xbaxbf', '兆', out) # 去掉最后的零 out = re.sub(r'(xe9x9bxb6)$', '', out) return out print(num2zh(1234))