还能再简化一点
def cn_digit(s):
if isinstance(s, (int, float)):
s = str(s)
elif not isinstance(s, str):
return s
s = s.replace(',', '')
m = re.match(r'(-?)(\d+)((?:\.\d+)?)', s, re.A)
if not m:
return s
intpart = m.group(2)
dem = "万亿兆京垓秭穰沟涧正载极"
thousands = intpart[-4:]
cn_dig = [intpart[i-4:i] + dem[-i//4-1] for i in range(-4,-min(len(intpart), 4*(len(dem)+1)),-4)]
return m.group(1) + intpart[0:-4*(len(dem)+1)] + ''.join(reversed(cn_dig)) + thousands + m.group(3)
【 在 moudy 的大作中提到: 】
: 嗯,应该把整数部分再劈成两半。
: [code=python]
: def cn_digit(s):
: ...................
--
FROM 171.221.29.*