Show file sizes with text and suffix Last updated: 03. Feb 2023
#JS example that shows a file size in a nice formatted way (bytes, KB, MB, GB and TB).
Example
Code Format-file-size-example.hash
#{
function formatSize(bytes)
{
for(let suffix of ["B", "KB", "MB", "GB"]) {
if (bytes < 1000) return bytes + " " + suffix;
bytes = parseInt(Math.round(bytes / 1000));
}
return bytes + " TB";
}
write(formatSize(100) + "\n");
write(formatSize(123200) + "\n");
write(formatSize(12325400) + "\n");
write(formatSize(1000000000) + "\n");
write(formatSize(1000000000000) + "\n");
}#
Output
100 B
123 KB
12 MB
1 GB
1 TB