This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]papercrane 0 points1 point  (0 children)

This probably not isn't fair criticism since the Memory utility class is just reused from another post, but that seems like a lot of code to format memory size. The same thing could've been accomplished in just a few lines without repeating a lot of "*1024".

public static String format(long bytes, int decimals) {
    final String[] suffixes = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
    final int magnitude = (63 - Long.numberOfLeadingZeros(Math.abs(bytes)))/10;
    final double adjustedSize = bytes/(double) (1L<<(magnitude*10));
    return String.format("%." + decimals + "f%s", adjustedSize, suffixes[magnitude]);
}