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'))
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)
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 dem = "万亿兆京垓秭穰沟涧正载极" cn_dig = [v + dem[i//4-1] if i > 1 and i % 4 == 0 and i // 4 <= len(dem) else v for i,v in enumerate(m.group(2)[::-1])] return m.group(1) + ''.join(reversed(cn_dig)) + m.group(3)