字节数转为人类易读大小和单位

def bit2humanView(bit_val):
    '''
    文件大小bit转为人类易读大小bit、KB、MB
    :param bit_val:字节数值
    :return:人类易读大小和单位
    '''
    is2kb = int(bit_val / 1042)  # 转换为kb取整
    is2mb = int(bit_val / 1024 / 1024)  # 转为mb取整
    is2gb = int(bit_val / 1024 / 1024 / 1024)  # 转为gb取整
    if is2gb is not 0:
        gb_val = bit_val / 1024 / 1024 / 1024
        return "%.2f GB" % gb_val
    if is2mb is not 0:
        mb_val = bit_val / 1024 / 1024
        return "%.2f MB" % mb_val
    if is2kb is not 0:
        kb_val = bit_val / 1024
        return "%.2f KB" % kb_val
    return "%s bit" % bit_val

  

posted @ 2018-09-17 15:49  安迪9468  阅读(221)  评论(0编辑  收藏  举报