用python 将数字每三组分割

时间:2021-04-06 20:37:51   收藏:0   阅读:19
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

原文:https://www.cnblogs.com/cherylgi/p/14623265.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!