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(long size)
format File Size
double bytes = size;
double kBytes = 0.0;
double mBytes = 0.0;
double gBytes = 0.0;
if (bytes >= 1024.0) {
    kBytes = bytes / 1024.0;
    if (kBytes >= 1024.0) {
        mBytes = kBytes / 1024.0;
...
StringformatFileSize(long size)
formatFileSize Convert a int representation into the label of file size.
StringBuffer str = new StringBuffer();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
if (size <= 0) {
    return "0 bytes";
if (size < 1024) {
...
StringformatFileSize(Long sizeBytes)
format File Size
String size = "";
try {
    double dSize = (double) sizeBytes;
    if (dSize >= 1024) {
        dSize /= 1024;
        size = "K";
        if (dSize >= 1024) {
            dSize /= 1024;
...
StringformatFilesizeGB(long filesize, int fractionDigits)
Formats filesize in bytes to GB
numberFormat.setMaximumFractionDigits(fractionDigits);
return new StringBuffer(numberFormat.format(filesize / 1073741824.0)).append(" GB").toString();
StringformatGameSize(int size)
format Game Size
DecimalFormat df = new DecimalFormat("0.00");
if (size > 1024 * 1024) {
    return String.valueOf(df.format((float) size / (1024 * 1024))) + "M";
} else {
    return String.valueOf(df.format((float) size / 1024)) + "K";
StringformatMemorySize(long size)
format Memory Size
long sizeInKB;
if (size == 0) {
    sizeInKB = 0;
} else {
    sizeInKB = size / 1024;
    if (size % 1024 != 0) {
        sizeInKB++;
    if (sizeInKB > 10 * 1024) {
        long sizeInMB = size / (1024 * 1024);
        return "" + memoryFormat.format(sizeInMB) + " MB";
return "" + memoryFormat.format(sizeInKB) + " KB";
StringformatPartSize(int size, NumberFormat format)
Format the size of a part like message or attachment.
String value = null;
if (size >= 1024) {
    value = format.format(size / 1024) + " KB";
} else {
    if (size > 0) {
        value = Integer.toString(size) + " B";
    } else {
        value = "n/a";
...
StringformatSize(double fileSize)
format Size
DecimalFormat onePlace = new DecimalFormat("0.0");
String result = "";
result = "<h1>" + (int) fileSize + "</h1> bytes";
double length = 0.0;
if (fileSize > 1024) {
    length = fileSize / 1024;
    result = "<h1>" + onePlace.format(length) + "</h1> kilo bytes";
if (fileSize > 1048576) {
    length = fileSize / 1048576;
    result = "<h1>" + onePlace.format(length) + "</h1> mega bytes";
if (fileSize > 1073741824) {
    length = fileSize / 1073741824;
    result = "<h1>" + onePlace.format(length) + "</h1> giga bytes";
return result;
StringformatSize(double size)
Format as a size
return doubleSizeFormatter.format(size);
StringformatSize(int size)
Formats a file size in bytes into a human readable file size, e.g 10,4 MB etc.
if (size <= 0)
    return "0";
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];