数字分隔符用逗号还是西方那套,而用汉字做分隔符可以直读,无论多长数字。
def cn_digit(s):
if isinstance(s, str):
s = s.replace(',', '')
if not re.match(r'\d+|\d+\.\d+', s, re.A):
return s
elif isinstance(s, (int, float)):
s = str(s)
else:
return s
r = s[::-1]
dot = r.find('.')
c = r[:dot+1]
r = r[dot+1:]
s = ''
if r[-1] == '-':
s = '-'
r = r[:-1]
d = 0
dem = "万亿兆京垓秭穰沟涧正载极"
while len(r) > 4:
c += r[:4] + dem[d]
d += 1
r = r[4:]
if d > len(dem) - 1:
break
return (c+r+s)[::-1]
print(cn_digit(3234912341.20))
print(cn_digit('56156874656748956441334964654654946546465246549846.95'))
32亿3491万2341.2
56极1568载7465正6748涧9564沟4133穰4964秭6546垓5494京6546兆4652亿4654万9846.95
--
FROM 110.185.26.*