convert_util.py 333 B

12345678910111213
  1. def byte_convert(bytes: int):
  2. # 定义单位
  3. units = ["Bytes", "KB", "MB", "GB", "TB"]
  4. unit_index = 0
  5. # 迭代转换单位,直到值小于1024
  6. while bytes >= 1024 and unit_index < len(units) - 1:
  7. bytes /= 1024.0
  8. unit_index += 1
  9. # 返回结果
  10. return f"{bytes:.2f}{units[unit_index]}"