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

NumberFormatcreateSizeFormat(Locale locale)
create Size Format
NumberFormat sizeFormat = DecimalFormat.getNumberInstance(locale);
sizeFormat.setGroupingUsed(true);
sizeFormat.setMaximumFractionDigits(0);
sizeFormat.setMinimumFractionDigits(0);
return sizeFormat;
StringgetBytesSpecificationDescription(double sizeInBytes, String formatSpec)
Returns a short string description of the size, in bytes, specified.
DecimalFormat df = new DecimalFormat(formatSpec);
df.setDecimalSeparatorAlwaysShown(false);
double teraBytes = sizeInBytes / TERA_BYTES;
if (teraBytes >= 1.0d) {
    return df.format(teraBytes) + " TB";
double gigaBytes = sizeInBytes / GIGA_BYTES;
if (gigaBytes >= 1.0d) {
...
StringgetByteStr(long size)
Return nice byte size, ex.
if (size < 1024)
    return "" + size + " byte" + (size == 1 ? "" : "s");
else if (size < 1024 * 1024)
    return "" + new DecimalFormat("##0.00").format(size / 1024.) + " kB";
else
    return "" + new DecimalFormat("##0.00").format(size / (1024. * 1024.)) + " MB";
StringgetDataSize(long size)
get Data Size
if (size < 1024) {
    return size + "bytes";
} else if (size < 1024L * 1024) {
    double kbsize = size / 1024D;
    return formater.format(kbsize) + "KB";
} else if (size < 1024L * 1024L * 1024) {
    double mbsize = size / 1024D / 1024D;
    return formater.format(mbsize) + "MB";
...
IntegergetFontSizeInPoints(String fontSizeWithUnit)
get Font Size In Points
fontSizeWithUnit = fontSizeWithUnit.trim();
DecimalFormat formatter = new DecimalFormat();
DecimalFormatSymbols formatterSymbol = new DecimalFormatSymbols();
formatterSymbol.setDecimalSeparator('.');
formatter.setDecimalFormatSymbols(formatterSymbol);
ParsePosition position = new ParsePosition(0);
Number size = formatter.parse(fontSizeWithUnit, position);
if (size == null)
...
StringgetFormatSize(double size)
get Format Size
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
int i = 0;
String[] unit = FILE_SIZE_UNIT;
for (i = 0; i < unit.length; i++) {
    if ((long) (size / 1024) > 0) {
        size /= 1024;
    } else {
...
StringgetFormatSize(long size)
get Format Size
double d = size;
if (size < KB_SCALE) {
    return size + " B";
if (size < MB_SCALE) {
    return df.format(d / KB_SCALE) + " KB";
if (size < GB_SCALE) {
...
StringgetFormattedSize(long size)
get Formatted Size
StringBuilder sb = new StringBuilder();
float megabytes = size / MEGABYTE;
if (megabytes > 1) {
    sb.append(formatter.format(megabytes));
    sb.append(" MB");
} else {
    float kilobytes = size / KILOBYTE;
    sb.append(formatter.format(kilobytes));
...
StringgetReadableByteSize(long size)
get Readable Byte Size
if (size <= 0) {
    return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
StringgetReadableSize(long size)
get Readable Size
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
final int unitIndex = (int) (Math.log10(size) / 3);
final double unitValue = 1 << (unitIndex * 10);
final String readableSize = new DecimalFormat("#,##0.#").format(size / unitValue) + " " + units[unitIndex];
return readableSize;