Deviopath › Node.js

Format File Size

Easy · Formatting

Write a function formatBytes(bytes) that makes a file size readable, using base 1024 (1 KB = 1024 B):

- below 1024: whole bytes, like "512 B"

- otherwise use the largest unit from KB, MB, GB where the value is at least 1, and show one decimal with toFixed(1): "1.5 KB", "3.0 MB"

- GB is the largest unit, so huge sizes stay in GB

Next: Promise Delay