Better display of stats

This commit is contained in:
Laurent Clouet
2024-12-17 15:00:33 +01:00
parent b3bef713fb
commit c2d953d475
2 changed files with 7 additions and 5 deletions

View File

@@ -24,14 +24,14 @@ public class TransferStats {
}
public String getSessionTraffic() {
return Utils.formatDataConsumption(this.bytes);
return Utils.formatDataConsumption(this.bytes, 0);
}
public String getAverageSessionSpeed() {
try {
return Utils.formatDataConsumption((long) (this.bytes / (this.millis / 1000f)));
return Utils.formatDataConsumption((long) (this.bytes / (this.millis / 1000f)), 1);
} catch (ArithmeticException e) { // Unlikely, but potential division by zero fallback if first transfer is done in zero millis
return Utils.formatDataConsumption((long) (this.bytes / (0.1f)));
return Utils.formatDataConsumption((long) (this.bytes / (0.1f)), 1);
}
}

View File

@@ -312,13 +312,14 @@ public class Utils {
/**
* Format a number of bytes into a human-readable format with metric prefixes (base 1024)
* @param bytes The amount of bytes to convert
* @param precision how many digit after the separator are displayed
* @return Human-readable formatted string (respecting locale of the machine)
* representing the amount of bytes
* Examples: 4.20GB, 20.00TB, 3.69MB
* In a different locale (here for German):
* Examples: 4,20GB, 20,00TB, 3,69MB
*/
public static String formatDataConsumption(long bytes) {
public static String formatDataConsumption(long bytes, int precision) {
float divider;
String suffix;
@@ -335,7 +336,8 @@ public class Utils {
suffix = "MB";
}
return String.format("%.2f%s", (bytes / divider), suffix);
String pattern = String.format("%%.%df%%s", precision);
return String.format(pattern, (bytes / divider), suffix);
}
/**