formatters.ts 347 B

1234567891011
  1. export const formatBytes = (bytes: number, decimals = 2) => {
  2. if (bytes === 0) return '0 Bytes';
  3. const k = 1024;
  4. const dm = decimals < 0 ? 0 : decimals;
  5. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  6. const i = Math.floor(Math.log(bytes) / Math.log(k));
  7. return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  8. };