size.ts 375 B

123456789101112
  1. export function convertBytesToHumanReadable(
  2. bytes: number,
  3. precision = 2,
  4. ): string {
  5. if (bytes === 0 || isNaN(bytes)) {
  6. return "0 MB";
  7. }
  8. const i = Math.floor(Math.log(bytes) / Math.log(1024));
  9. const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  10. return (bytes / Math.pow(1024, i)).toFixed(precision) + " " + sizes[i];
  11. }