1
0
Fork 0
mirror of https://github.com/CorentinTh/it-tools.git synced 2025-08-02 03:55:17 +02:00
it-tools/src/utils/convert.ts
2022-04-14 01:06:06 +02:00

11 lines
340 B
TypeScript

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 parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
}