1const formatBytes = (bytes, decimals = 2) => {
2 if (bytes === 0) return '0 Bytes';
3
4 const k = 1024;
5 const dm = decimals < 0 ? 0 : decimals;
6 const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
7 const i = Math.floor(Math.log(bytes) / Math.log(k));
8
9 return (
10 parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]
11 );
12}
1function formatBytes(bytes, decimals = 2) {
2 if (bytes === 0) return '0 Bytes';
3
4 const k = 1024;
5 const dm = decimals < 0 ? 0 : decimals;
6 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
7
8 const i = Math.floor(Math.log(bytes) / Math.log(k));
9
10 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
11}
1const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2
3function niceBytes(x){
4
5 let l = 0, n = parseInt(x, 10) || 0;
6
7 while(n >= 1024 && ++l){
8 n = n/1024;
9 }
10 //include a decimal point and a tenths-place digit if presenting
11 //less than ten of KB or greater units
12 return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
13}
1function bytesToSize(bytes) {
2 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
3 if (bytes === 0) return 'n/a'
4 const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
5 if (i === 0) return `${bytes} ${sizes[i]})`
6 return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
7}