def cut_num(n, sep=','): # 第一个参数是要传递的数字,第二个是分隔符 s = str(abs(n))[::-1] groups = [] i = 0 while i < len(s): groups.append(s[i:i + 3]) i += 3 retval = sep.join(groups)[::-1] if n < 0: return '-%s' % retval else: return retval
转载: https://blog.51cto.com/haoyonghui/2133156