Java Utililty Methods Size

List of utility methods to do Size

Description

The list of methods to do Size are organized into topic(s).

Method

StringgetSize(long octets)
get Size
String unit;
double div;
if (octets < MB) {
    unit = "KB";
    div = KB;
} else if (octets < GB) {
    unit = "MB";
    div = MB;
...
StringgetSizeAll(final long bytes)
get Size All
return humanReadableByteCount(bytes, true) + " " + humanReadableByteCount(bytes, false) + " "
        + formatNumber(bytes) + " B";
NumberFormatgetSizeFormat()
get Size Format
return sNumberFormat.get();
StringgetSizeInKB(String size)
get Size In KB
if (size.indexOf("-") != -1 || size.equals("0"))
    return "0";
String s = size.substring(0, size.length() - 1);
double ds = 0;
try {
    ds = Double.valueOf(s);
} catch (NumberFormatException e) {
    System.out.println("NFE getSizeInKB str: " + size);
...
StringgetSizeString(long bytes)
Create a formatted string with the given number of bytes in the highest reasonable unit, like 512 B, 1,3 KB or 2 MB.
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
if (Math.abs(bytes) < 1024) {
    return bytes + " B";
if (Math.abs(bytes) >= 1024 * 1024) {
    float megaBytes = (float) bytes / (1024 * 1024);
    return nf.format(megaBytes) + " MB";
float kiloBytes = (float) bytes / 1024;
return nf.format(kiloBytes) + " KB";
StringgetSizeString(long number, Locale locale)
get Size String
String suffix;
double sz;
if (number == 0) {
    return "0";
} else if (number >= GIB) {
    sz = (number / GIB);
    suffix = "GB";
} else if (number >= MIB) {
...
StringgetSizeString(long size)
get Size String
if (size < MSIZE) {
    DecimalFormat format = new DecimalFormat("0.0KB");
    return format.format((double) size / KSIZE);
if (size < GSIZE) {
    DecimalFormat format = new DecimalFormat("0.0MB");
    return format.format((double) size / MSIZE);
if (size < TSIZE) {
    DecimalFormat format = new DecimalFormat("0.0GB");
    return format.format((double) size / GSIZE);
DecimalFormat format = new DecimalFormat("0.0TB");
return format.format((double) size / TSIZE);
StringgetSizeText(long size)
get Size Text
DecimalFormat df = new DecimalFormat("0.00");
if (size >= 0L && size < 1024) {
    return size + "B";
} else if (size >= 1024 && size < 1048576) {
    double fileSize = (double) size / 1024;
    return df.format(fileSize) + "KB";
} else {
    double fileSize = (double) size / 1048576;
...
StringgetSpaceSizeFromByte(long xB)
get Space Size From Byte
String ret = "";
DecimalFormat myFormatter = new DecimalFormat("###.###");
double xBd = (double) xB;
long Hi = 1;
Hi = Hi << 60;
long Pi = 1;
Pi = Pi << 50;
double Pd = (double) Pi;
...
floatgetStringSize(String string)
get String Size
float size = 0f;
if (string.length() > 0) {
    size = string.length() / 1024f;
size = size * 100;
if (size > (int) size) {
    size = size + 1;
size = size / 100f;
DecimalFormat format = new DecimalFormat("##.##");
return Float.parseFloat(format.format(size));