diff --git a/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java b/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java index b22251a51..b37bd48df 100644 --- a/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java +++ b/app/src/main/java/io/xpipe/app/browser/BrowserFileListComp.java @@ -445,7 +445,7 @@ final class BrowserFileListComp extends SimpleComp { if (path.getRawFileEntry().resolved().getKind() == FileKind.DIRECTORY) { setText(""); } else { - setText(byteCount(fileSize.longValue(), true)); + setText(byteCount(fileSize.longValue())); } } } diff --git a/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java b/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java index a16d3cfcc..148e089ee 100644 --- a/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java +++ b/app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java @@ -47,8 +47,8 @@ public class BrowserStatusBarComp extends SimpleComp { if (p == null || p.done()) { return null; } else { - var transferred = HumanReadableFormat.byteCount(p.getTransferred(), false); - var all = HumanReadableFormat.byteCount(p.getTotal(), true); + var transferred = HumanReadableFormat.progressByteCount(p.getTransferred()); + var all = HumanReadableFormat.byteCount(p.getTotal()); var name = (p.getName() != null ? " @ " + p.getName() + " " : ""); return transferred + " / " + all + name; } diff --git a/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java b/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java index 1d3c915ab..626e2c0c3 100644 --- a/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java +++ b/app/src/main/java/io/xpipe/app/util/HumanReadableFormat.java @@ -16,17 +16,34 @@ public final class HumanReadableFormat { public static final DateTimeFormatter DAY_OF_WEEK = DateTimeFormatter.ofPattern("EEE"); public static final DateTimeFormatter HOUR_MINUTE = DateTimeFormatter.ofPattern("HH:mm"); - public static String byteCount(long bytes, boolean precise) { - if (-1024 < bytes && bytes < 1024) { + public static String byteCount(long bytes) { + var b = 1024; + if (-b < bytes && bytes < b) { return bytes + " B"; } CharacterIterator ci = new StringCharacterIterator("kMGTPE"); - while (bytes <= -1024 * 1024 || bytes >= 1024 * 1024) { - bytes /= 1024; + var mb = b * b; + while (bytes <= -mb || bytes >= mb) { + bytes /= b; ci.next(); } - var f = precise ? "%.1f" : "%.0f"; - return String.format(f + " %cB", bytes / 1024.0, ci.current()); + var f = "%.1f"; + return String.format(f + " %cB", bytes / (double) b, ci.current()); + } + + public static String progressByteCount(long bytes) { + var b = 1024; + if (-b < bytes && bytes < b) { + return bytes + " B"; + } + CharacterIterator ci = new StringCharacterIterator("kMGTPE"); + var mb = b * b; + while (bytes <= -mb || bytes >= mb) { + bytes /= b; + ci.next(); + } + var f = ci.getIndex() >= 2 ? "%.3f" : "%.0f"; + return String.format(f + " %cB", bytes / (double) b, ci.current()); } public static String date(LocalDateTime x) {