Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong File Size conversion in GB #22

Open
prasunkhan opened this issue May 26, 2024 · 2 comments
Open

Wrong File Size conversion in GB #22

prasunkhan opened this issue May 26, 2024 · 2 comments

Comments

@prasunkhan
Copy link

If the true file size is 1,073,741,824 B, Disky displays its size as 1.074GB (i.e. simply divide the size by 1000 while converting bytes to KB, KB to MB and MB to GB).

However, the true size in GB is just 1GB, i.e. 1,073,741,824 / (1,024 * 1,024 * 1,024) GB.

@ParitoshKumarTripathi
Copy link

Yes, this file needs modification - app/src/main/java/de/felixnuesse/disky/extensions/fs.kt

This is the corrected code. (The changes being changing log10(1000) to log10(1024) and 1000.0.pow to 1024.0.pow -

//https://stackoverflow.com/a/5599842
fun Any.readableFileSize(size: Long): String {
if (size <= 0) return "0"
val units = arrayOf("B", "KB", "MB", "GB", "TB")
val digitGroups = (log10(size.toDouble()) / log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(size / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups]
}

@lord-ne
Copy link

lord-ne commented Oct 13, 2024

Whether 1GB means 1000^3 or 1024^3 bytes is a matter of debate. Really the SI prefix "Giga" is supposed to always mean 1000^3, but 1024^3 is generally a much more useful measurement when dealing with disk sizes (because it's a power of 2).

A solution would be to use the IEC binary prefixes: KiB, MiB, GiB, TiB, etc. These are unambiguous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants