formatbytes.js 368 B

1234567891011
  1. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  2. export default function formatBytes(bytes, decimals) {
  3. if (bytes === 0) return '0 Byte';
  4. let k = 1000;
  5. let value = Math.floor(Math.log(bytes) / Math.log(k));
  6. let decimal = decimals + 1 || 3;
  7. return (bytes / Math.pow(k, value)).toPrecision(decimal) + ' ' + sizes[value];
  8. }