mirror of
https://github.com/CorentinTh/it-tools.git
synced 2026-06-29 04:32:23 +00:00
11 lines
332 B
TypeScript
11 lines
332 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 / k ** i).toFixed(decimals))} ${sizes[i]}`;
|
|
}
|