aboutsummaryrefslogtreecommitdiff
path: root/src/utils/convert.ts
blob: 9eac19212ffdab4bab7f54d5adecd4df58b7915b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
export function formatBytes(bytes: number, decimals = 2) {
  if (bytes === 0) {
    return '0 Bytes';
  }

  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return `${Number.parseFloat((bytes / k ** i).toFixed(decimals))} ${sizes[i]}`;
}