Skip to content

Commit b733ce3

Browse files
dougxcgilles-duboscq
authored andcommitted
Remove assertion in ByteFormattingUtil#toHuman and restore floating point precision
1 parent 482fcb2 commit b733ce3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/ByteFormattingUtil.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
package com.oracle.svm.core.util;
2626

2727
public class ByteFormattingUtil {
28-
// "123.12KiB".length() = 9, holds as long as it's not >= 1000GiB
28+
/**
29+
* {@code "123.12KiB".length() = 9} almost always holds as long as the value is not >= 1000GiB
30+
* and {@link String#format} floating point rounding does not bump the whole part to 4 digits
31+
* (e.g. {@code String.format("%.2f", 999.995D) == "1000.00"}).
32+
*/
2933
private static final int MAX_WIDTH = 9;
34+
3035
public static final String RIGHT_ALIGNED_FORMAT = "%" + MAX_WIDTH + "s";
3136

3237
private enum Unit {
@@ -61,9 +66,6 @@ public static String bytesToHumanGB(long bytes) {
6166
}
6267

6368
private static String toHuman(long value, Unit unit) {
64-
long scaled = value / unit.value;
65-
String string = "%.2f%s".formatted((double) scaled, unit.toString());
66-
assert string.length() <= MAX_WIDTH || value >= 1000L * Unit.GiB.value : value;
67-
return string;
69+
return "%.2f%s".formatted((double) value / (double) unit.value, unit.toString());
6870
}
6971
}

0 commit comments

Comments
 (0)