Java Utililty Methods Size Format

List of utility methods to do Size Format

Description

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

Method

StringformatSize(long bytes)
Caller should append 'B' or 'b' as appropriate NOTE: formatDuration2() recommended in most cases for readability
double val = bytes;
int scale = 0;
while (val >= 1024) {
    scale++;
    val /= 1024;
DecimalFormat fmt = new DecimalFormat("##0.00");
String str = fmt.format(val);
...
StringformatSize(long bytes)
format Size
if (bytes < 1024)
    return bytes + " B";
else if (bytes < 1024 * 1024)
    return singleDecimalDigit(bytes / 1024.0) + " KB";
else if (bytes < 1024 * 1024 * 1024)
    return singleDecimalDigit(bytes / 1024.0 / 1024.0) + " MB";
else
    return singleDecimalDigit(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
...
StringformatSize(long bytes)
format Size
DecimalFormat df = new DecimalFormat(".0");
if (bytes > TB)
    return df.format(bytes * 1.0d / TB) + "TB";
if (bytes > GB)
    return df.format(bytes * 1.0d / GB) + "GB";
if (bytes > MB)
    return df.format(bytes * 1.0d / MB) + "MB";
if (bytes > KB)
...
StringformatSize(long fileSize)
format Size
if (fileSize < 1024) {
    return String.valueOf(fileSize) + "bytes";
} else if (fileSize >= 1024 && fileSize < 1048576) {
    float temp = (float) fileSize / 1024;
    return df.format(temp) + "Kb";
} else if (fileSize >= 1048576 && fileSize < 1073741824) {
    float temp = (float) fileSize / 1048576;
    return df.format(temp) + "Mb";
...
StringformatSize(long longSize)
format Size
return formatSize(longSize, -1);
StringformatSize(long longSize, int decimalPos)
Formats a file size
NumberFormat fmt = NumberFormat.getNumberInstance();
if (decimalPos >= 0) {
    fmt.setMaximumFractionDigits(decimalPos);
final double size = longSize;
double val = size / (1024 * 1024 * 1024);
if (val > 1) {
    return fmt.format(val).concat(" GB");
...
StringformatSize(long size)
Format the document size for human readers
DecimalFormat df = new DecimalFormat("#0.0");
String str;
if (size / 1024 < 1) {
    str = size + " B";
} else if (size / 1048576 < 1) {
    str = df.format(size / 1024.0) + " KB";
} else if (size / 1073741824 < 1) {
    str = df.format(size / 1048576.0) + " MB";
...
StringformatSize(long size)
Formats a file size as a human-readable string.
if (size < 1024) {
    return size + " bytes";
double sz = size;
String units;
double nsz;
String format = "0";
if (size >= GIGABYTE) {
...
StringformatSize(long size)
format Size
if (size > 1000000000)
    return DecimalFormat.getNumberInstance().format(size / 1000000000) + " GB";
if (size > 1000000)
    return DecimalFormat.getNumberInstance().format(size / 1000000) + " MB";
if (size > 1000)
    return DecimalFormat.getNumberInstance().format(size / 1000) + " KB";
return DecimalFormat.getNumberInstance().format(size) + " B";
StringformatSize(long size)
format Size
DecimalFormat df = new DecimalFormat("###,###.#");
long div = 1;
long testval = size;
for (int i = 0; i < 6; i++) {
    if ((testval >>>= 10) == 0) {
        return df.format((double) size / div) + units[i];
    div <<= 10;
...