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

StringformatFileSize(File file)
format File Size
return formatFileSize(file.length());
StringformatFilesize(final long filesize, final Locale locale)
Returns the formatted file size to Bytes, KB, MB or GB depending on the given value.
String result;
final long filesizeNormal = Math.abs(filesize);
if (Math.abs(filesize) < 1024) {
    result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_BYTES_1"),
            new Object[] { new Long(filesizeNormal) });
} else if (filesizeNormal < 1048576) {
    result = MessageFormat.format(m_bundle.getString("GUI_FILEUTIL_FILESIZE_KBYTES_1"),
            new Object[] { new Double(filesizeNormal / 1024.0) });
...
StringformatFilesize(int s)
Format file size
if (s < 1024) {
    return s + "B";
} else {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(1);
    nf.setMinimumFractionDigits(1);
    if (s < (1024 * 1024)) {
        return nf.format((double) s / 1024) + "KB";
...
StringformatFileSize(long fileS)
format File Size
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
    fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
    fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
    fileSizeString = df.format((double) fileS / 1048576) + "MB";
...
StringformatFilesize(long filesize)
Formats filesize in bytes as appropriate to Bytes, KB, MB or GB
String result;
if (Math.abs(filesize) < 1024) {
    result = "" + filesize + " Bytes";
} else if (Math.abs(filesize) < 1048576) {
    result = formatFilesizeKB(filesize, 2);
} else if (Math.abs(filesize) < 1073741824) {
    result = formatFilesizeMB(filesize, 2);
} else {
...
StringformatFileSize(long fileSize, int decimalPos)
Formats a file size in hmnan readable form.
NumberFormat fmt = NumberFormat.getNumberInstance();
if (decimalPos >= 0) {
    fmt.setMaximumFractionDigits(decimalPos);
String formattedSize;
final double size = fileSize;
double val = size / (BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE);
if (val > 1) {
...
StringformatFileSize(long length)
Formats the given file size into a nice string (123 bytes, 10.6 KB, 1.2 MB).
if (length < 1024)
    return length + " bytes";
else if (length < 1024 * 1024)
    return KB_FORMAT.format((double) length / 1024);
else
    return MB_FORMAT.format((double) length / 1024 / 1024);
StringformatFileSize(long size)
create a formatted filesize string
float tmpSize = size;
int idx = 0;
while (tmpSize > 1024) {
    tmpSize /= 1024.0f;
    ++idx;
return FSIZE.format(tmpSize) + SIZE_UNIT[idx];
StringformatFileSize(long size)
format File Size
double bytes = (double) size;
if (bytes < 1024) {
    return String.valueOf((int) bytes) + " bytes";
} else if (bytes < 1048576) {
    return FILE_SIZE_FORMAT.format(bytes / 1024) + " K";
} else {
    return FILE_SIZE_FORMAT.format(bytes / 1024 / 1024) + " MB";
StringformatFileSize(long size)
Takes a string with a byte count and converts it into a "nice" representation of size.
if (size < 1024)
    return size + " B";
else if (size < 1024 * 1024)
    return new DecimalFormat("#.# KB").format((double) size / 1024);
else if (size < 1024 * 1024 * 1024)
    return new DecimalFormat("#.# MB").format((double) size / 1024 / 1024);
return new DecimalFormat("#.# GB").format((double) size / 1024 / 1024 / 1024);