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

StringbyteCountToDisplaySize(long bytes)
Formats the given number of bytes to a human-readable representation up to GiB units.

This method will provide higher accuracy than the same one found in commons-io, by offering 2 decimal places for GiB and MiB and 1 decimal place for KiB ranges.
if (bytes / ONE_GIB > 0) {
    return GIBI_BYTE_FORMAT.format((double) bytes / ONE_GIB);
if (bytes / ONE_MIB > 0) {
    return MEBI_BYTE_FORMAT.format((double) bytes / ONE_MIB);
if (bytes / ONE_KIB > 0) {
    return KIBI_BYTE_FORMAT.format((double) bytes / ONE_KIB);
...
StringbyteCountToDisplaySize(long size)
byte Count To Display Size
int MB = 1024 * 1024;
return MessageFormat.format("{0} MB", size / MB);
StringbyteCountToDisplaySize(long size)
Get the display of the given byte size.
String displaySize;
DecimalFormat format = new DecimalFormat("###0.0");
if (size / ONE_GB > 0) {
    displaySize = format.format((double) size / ONE_GB) + "GB";
} else if (size / ONE_MB > 0) {
    displaySize = format.format((double) size / ONE_MB) + "MB";
} else if (size / ONE_KB > 0) {
    displaySize = format.format((double) size / ONE_KB) + "KB";
...
StringbyteCountToDisplaySize(long size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
double ONE_KiB_D = 1024D;
double ONE_MiB_D = ONE_KiB_D * ONE_KiB_D;
double ONE_GiB_D = ONE_KiB_D * ONE_MiB_D;
StringBuilder sbResult = new StringBuilder();
NumberFormat sizeFormat = NumberFormat.getNumberInstance();
sizeFormat.setMinimumFractionDigits(0);
sizeFormat.setMaximumFractionDigits(2);
if (size > ONE_GiB_D) {
...
StringbyteSizeString(long bytes)
Returns a String representing the given bytes, with a textual representation depending if the given amount can be represented as KB, MB, GB or TB
if (bytes < 0)
    throw new IllegalArgumentException("Given bytes can't be less than zero");
String sizeText;
String[] bytesUnits = { "B", "KB", "MB", "GB", "TB" };
long bytesAmount = bytes;
short binRemainder;
float decRemainder = 0;
int u;
...
StringBytesToHRSize(String len)
Bytes To HR Size
if (len.length() < 5) {
    if (Integer.parseInt(len) <= 1024) {
        return len + " bytes";
DecimalFormat format = new DecimalFormat("0.000");
double newlen = Double.parseDouble(len);
newlen = newlen / 1024.0;
...
StringbytesToSizeFormat(long bytes)
bytes To Size Format
if (bytes < 1024) {
    return bytes + " B";
NumberFormat f = NumberFormat.getNumberInstance(new Locale("nl"));
f.setMaximumFractionDigits(1);
f.setMinimumFractionDigits(1);
if (bytes < (1024 * 1024)) {
    return f.format(bytes / 1024.0) + " kB";
...
StringbytesToString(long sizeInBytes)
bytes To String
final double SPACE_KB = 1024;
final double SPACE_MB = 1024 * SPACE_KB;
final double SPACE_GB = 1024 * SPACE_MB;
final double SPACE_TB = 1024 * SPACE_GB;
NumberFormat nf = new DecimalFormat();
nf.setMaximumFractionDigits(2);
try {
    if (sizeInBytes < SPACE_KB) {
...
StringbytesToUnits(long size)
bytes To Units
String hrSize = null;
double b = size;
double k = size / 1024.0;
double m = ((size / 1024.0) / 1024.0);
double g = (((size / 1024.0) / 1024.0) / 1024.0);
double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);
DecimalFormat dec = new DecimalFormat("0.00");
if (t > 1) {
...
StringcalcSize(double s, String type, int div)
calc Size
if (type.equals("MB")) {
    s = s / div; 
    s = s / div; 
    return NumberFormat.getNumberInstance().format(s) + " " + type;
} else if (type.equals("KB")) {
    s = s / div; 
    return NumberFormat.getNumberInstance().format(s) + " " + type;
} else if (type.equals("MBit")) {
...