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

Stringformat(long size)
format
DecimalFormat df = new DecimalFormat("0.00");
float sizeK = 1024.0f;
float sizeM = sizeK * sizeK;
float sizeG = sizeM * sizeK;
float sizeT = sizeG * sizeK;
if (size < sizeM) {
    return df.format(size / sizeK) + " KB";
if (size < sizeG) {
    return df.format(size / sizeM) + " MB";
if (size < sizeT) {
    return df.format(size / sizeG) + " GB";
return "";
StringformatBinarySize(final long l)
Formats a binary size.
if ((l & -l) != l)
    throw new IllegalArgumentException("Not a power of 2: " + l);
if (l >= (1L << 40))
    return format(l >> 40) + "Ti";
if (l >= (1L << 30))
    return format(l >> 30) + "Gi";
if (l >= (1L << 20))
    return format(l >> 20) + "Mi";
...
StringformatBytes(double size)
format Bytes
double k = size / 1024.0;
double m = k / 1024.0;
double g = m / 1024.0;
DecimalFormat dec = new DecimalFormat("0");
if (g > 1) {
    return dec.format(g).concat("G");
} else if (m > 1) {
    return dec.format(m).concat("M");
...
StringformatBytes(long size)
Displays the given in a human-readable format.
if (size <= 0) {
    return size + " B"; 
String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; 
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; 
StringformatByteSize(long bytes)
Returns a formatted string using an appropriate unit (Bytes, KB or MB).
if (bytes < 1024) {
    return numberFormat.format(bytes) + " Bytes";
float kb = (float) bytes / 1024;
if (kb < 1024) {
    return numberFormat.format(kb) + " KB";
float mb = kb / 1024;
...
StringformatDataSize(final double dataSize)
Formats the specified data size in human readable format.
final NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2);
double dataSizeToFormat = dataSize / BYTES_IN_TERABYTE;
if (dataSizeToFormat > 1) {
    return format.format(dataSizeToFormat) + " TB";
dataSizeToFormat = dataSize / BYTES_IN_GIGABYTE;
if (dataSizeToFormat > 1) {
...
StringformatDataSize(long bytes)
format Data Size
if (bytes <= 0)
    return "0 B";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digits = (int) (Math.log(bytes) / Math.log(1024));
return new DecimalFormat("#,##0.##").format(bytes / Math.pow(1024, digits)) + " " + units[digits];
StringformatDecimal(double size)
Format decimal numbers to #.##
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
return df.format(size);
StringformatDiskSize(final long diskSize)
format Disk Size
final float COUNT = 1000;
if (diskSize < 1) {
    return "n/a";
if (diskSize == 0) {
    return "0";
final NumberFormat nf = NumberFormat.getInstance();
...
StringformatFileSize(double fileSize, int precision, int unit, boolean showUnit)
Returns the file size.
String s = "#,";
for (int i = 0; i < precision; i++)
    s += "#";
DecimalFormat df = new DecimalFormat(s);
return applyDecimalFormat((long) fileSize, unit, df, showUnit);