17 lines
376 B
TypeScript
Raw Normal View History

2024-11-27 09:43:03 +01:00
export const Bytes = {
pretty(bytes: number) {
const sizes = ["B", "KB", "MB", "GB", "TB"];
2024-11-27 09:43:03 +01:00
if (bytes == 0) {
return "0 B";
2024-11-27 09:43:03 +01:00
}
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());
2024-08-22 17:41:44 +02:00
2024-11-27 09:43:03 +01:00
if (i == 0) {
return bytes.toFixed(1) + " " + sizes[i];
2024-11-27 09:43:03 +01:00
}
2024-08-22 17:41:44 +02:00
2024-11-27 09:43:03 +01:00
return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i];
2024-08-22 17:41:44 +02:00
}
2024-11-27 09:43:03 +01:00
}