Android Long Format getDisplayable(long numBytes)

Here you can find the source of getDisplayable(long numBytes)

Description

get Displayable

Declaration

public static String getDisplayable(long numBytes) 

Method Source Code

//package com.java2s;
import java.util.Formatter;

public class Main {
    private static final int BYTES_SCALING_FACTOR = 1024;

    public static String getDisplayable(long numBytes) {
        if (numBytes < BYTES_SCALING_FACTOR) {
            return numBytes + " bytes";
        }//from   w ww .j  ava 2s .c  o m
        String label;
        double size = ((double) numBytes) / BYTES_SCALING_FACTOR;
        if (size < BYTES_SCALING_FACTOR) {
            label = "Kb";
        } else {
            size = size / BYTES_SCALING_FACTOR;
            if (size < BYTES_SCALING_FACTOR) {
                label = "Mb";
            } else {
                size = size / BYTES_SCALING_FACTOR;
                label = "Gb";
            }
        }

        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb);

        formatter.format("%.2f %s", size, label);
        return sb.toString();
    }
}

Related

  1. formatPercent(long divisor, long dividend)
  2. formatSize(long value)
  3. getTimeFormattedFromMillis(long millis)
  4. formatFileSize(long length)
  5. ReadableByteCount(long bytes)
  6. formatByte(long l)
  7. formatFromSize(long size)