cloudservices/src/functions/dataConversion.ts

9 lines
281 B
TypeScript
Raw Normal View History

2019-11-09 23:34:36 -05:00
export default function dataConversion(bytes) {
2019-10-28 16:21:04 -04:00
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2019-11-09 23:34:36 -05:00
if (bytes === 0) {
return '0 KB';
}
2019-10-28 16:21:04 -04:00
return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`;
}