Java Utililty Methods Long Number Readable Format

List of utility methods to do Long Number Readable Format

Description

The list of methods to do Long Number Readable Format are organized into topic(s).

Method

StringbytesToHumanString(long bytes)
bytes To Human String
if (bytes < 0) {
    throw new IllegalArgumentException("Negative argument: " + bytes);
if (bytes == 1) {
    return "1 byte";
} else if (bytes == 0) {
    return MessageFormat.format("0{1}", bytes, suffixes[0]);
int log = (int) Math.log10(bytes);
int pos = log / 3;
double scaled = bytes / Math.pow(10, pos * 3);
long rounded = Math.round(scaled);
double rounded2 = (double) Math.round(scaled * 10) / 10d;
if (pos == 0) {
    return MessageFormat.format("{0,number,0}{1}", rounded, suffixes[pos]);
} else if (rounded >= 1000) {
    scaled = scaled / 1000;
    pos = pos + 1;
    rounded2 = (double) Math.round(scaled * 10) / 10d;
    return MessageFormat.format("{0,number,0.0}{1}", rounded2, suffixes[pos]);
} else if (rounded2 < 10) {
    return MessageFormat.format("{0,number,0.0}{1}", rounded2, suffixes[pos]);
} else {
    return MessageFormat.format("{0,number,0}{1}", rounded, suffixes[pos]);
StringbytesToString(long bytes)
Convert a number of bytes into a human readable string
if (bytes > 1024 * 1024 * 1024) {
    return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0 * 1024.0)) + " GB";
if (bytes > 1024 * 1024) {
    return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0)) + " MB";
if (bytes > 1024) {
    return new DecimalFormat("#.##").format((double) bytes / 1024.0) + " KB";
...
StringconvertBytes(long bytes)
Convert bytes in human readable format(means add KB, MB or GB according to the provided input.
String val = "0";
if (bytes == 0) {
    return val;
long KILOBYTE = 1024L;
long MEGABYTE = 1024L * 1024L;
long GIGABYTE = 1024L * 1024L * 1024L;
DecimalFormat df = new DecimalFormat("0.00");
...
StringconvertBytes(long size)
convert Bytes
if (size <= 0)
    return "0";
final 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];
StringconvertByteToGMKB(long bytes)
convert Byte To GMKB
String gmkb = null;
DecimalFormat df = new DecimalFormat("0.##");
if (bytes < 1024) {
    gmkb = String.valueOf(bytes) + "B";
} else if (bytes >= 1024 && bytes < 1024 * 1024) {
    double kb = (double) bytes / 1024;
    gmkb = df.format(kb) + "K";
} else if (bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) {
...
StringconvertByteUnit(Long l)
Converts a given number to byte units, i.e.
String s = null;
String[] unit = { "B", "KB", "MB", "GB", "TB" };
int i = 0;
if (l < 1024) {
    s = "" + l;
} else {
    float del = 1024.0f;
    float f = l;
...
StringconvertHumanSize(long byteSize)
Check whether file is executable according to its extenstion and executable extension name list from Edgenius configuaration.
String unit = "";
float size = (float) byteSize;
if (size > 1024) {
    size = size / 1024;
    unit = "K";
if (size > 1024) {
    size = size / 1024;
...
StringconvertLongToMega(long value)
convert Long To Mega
long megaValue = value / MEGA;
String result = Long.toString(megaValue) + " MB";
return result;
StringconvertMbIntoGb(long megaBytes)
convert Mb Into Gb
String convertedVal = "0";
if (megaBytes == 0) {
    return convertedVal;
final double HUNDRED = 100.0;
final long DIVEDEBY = 1024L;
DecimalFormat df = new DecimalFormat("0.00");
if (megaBytes / DIVEDEBY > 0) {
...
StringformatBytes(long bts)
format Bytes
long bt = bts % 1024;
long kb = (bts / 1024) % 1024;
long mb = bts / (1024 * 1024);
return ((mb > 0) ? (mb + "MB,") : "") + ((kb > 0) ? (kb + "KB,") : "") + ((bt > 0) ? (bt + "BYTES") : "");