This nice little tool triggered my inner geek
, and I started wondering about the different ways you could compute the size of something like a file or free space in KB/MB/GB, or perhaps
KiB/MiB/GiB.
If you look at different file managers and other tools, you will find they sometimes differ in what they report as file size and free space. Perhaps it is not as obvious as it may seem.
Let's assume 2 decimals for simplicity -- what should we report for 1000? 1023? 1024? One KiB is 1024 bytes, so 1000 bytes should be less than one, 1023 should be less than one, and 1024 should be exactly one. But at what point should we get 1.01 KiB?
We should get 1.01 KiB when the size is greater than or equal to 1024*1.01 = 1034.24 -- so not until 1035, because 1034 is less than 1.01 KiB.
But that means that a size of 10 or below will be reported as 0.00, because 10 is less than 1024*0.01 = 10.24
.
For free space, I think it makes sense to truncate the value like this, because having 10 bytes free is not having 0.01 KiB free. For file sizes on the other hand it seems counter intuitive to report a size of 0.00, and perhaps the value should be rounded up.
Anyway, I wonder if anybody has some input on what the "correct" way of doing this is?
I made a little test program in C which imitates your tool, so I could play around with how to do it -- I hope you don't mind if I post it here
.