hienlt0610
10/24/2019 - 4:44 AM

[Android] getMemoryUnit

public static String getMemoryUnit(long bytes) {
        DecimalFormat oneDecimal = new DecimalFormat("0.0");
        float BYTE = 1024.0f, KB = BYTE, MB = KB * KB, GB = MB * KB, TB = GB * KB;
        long absNumber = Math.abs(bytes);
        double result = bytes;
        String suffix = " Bytes";
        if (absNumber < MB) {
            result = bytes / KB;
            suffix = " KB";
        } else if (absNumber < GB) {
            result = bytes / MB;
            suffix = " MB";
        } else if (absNumber < TB) {
            result = bytes / GB;
            suffix = " GB";
        }
        return oneDecimal.format(result) + suffix;
    }