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

StringhumanReadableNumber(long total)
human Readable Number
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
return formatter.format(total);
StringhumanReadableSize(long byteCount)
Returns a human-readable (memory, file, ...) size.
String result;
if (byteCount / GB_BYTES > 0) {
    result = Long.toString(byteCount / GB_BYTES) + " GB";
} else if (byteCount / MB_BYTES > 0) {
    result = Long.toString(byteCount / MB_BYTES) + " MB";
} else if (byteCount / KB_BYTES > 0) {
    result = Long.toString(byteCount / KB_BYTES) + " kB";
} else {
...
StringhumanReadableSize(long bytes)
Human readable size conversion.

Credit: 'aioobe' at 'StackOverFlow.com'
int unit = 1024;
if (bytes < unit) {
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = ("KMGTPE").charAt(exp - 1) + ("i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
StringhumanReadableTime(final long duration)
human Readable Time
String unit = "";
long mins = duration / (60L * 1000);
long hr = mins / 60;
long yrs = hr / 24 / 365;
if (mins <= 0) {
    long secs = duration / 1000;
    unit = secs + " sec";
    if (secs > 1) {
...
StringhumanTimeDiff(long timeDiff)
human Time Diff
if (timeDiff < 1000) {
    return String.format("%dmsec", timeDiff);
StringBuilder buf = new StringBuilder();
long hours = timeDiff / (60 * 60 * 1000);
long rem = (timeDiff % (60 * 60 * 1000));
long minutes = rem / (60 * 1000);
rem = rem % (60 * 1000);
...
Stringreadable(long bytes)
readable
DecimalFormat df = new DecimalFormat("#.###");
if (bytes >= GB) {
    return df.format((double) bytes / GB) + "GB";
} else if (bytes >= MB) {
    return df.format((double) bytes / MB) + "MB";
} else if (bytes >= KB) {
    return df.format((double) bytes / KB) + "KB";
} else {
...
StringstringForBytes(long bytes)
Return a human-readable indication of a size, given a number of bytes.
DecimalFormat deci = new DecimalFormat("0.00");
double gigs = (((double) bytes / 1024d) / 1024d / 1024d);
double megs = (((double) bytes / 1024d) / 1024d);
double kb = ((double) bytes / 1024d);
if (gigs > 1) {
    return deci.format(gigs) + " GB";
if (megs > 1) {
...
Stringstringify(long byteNumber)
Formatter byte to kb,mb or gb etc.
if (byteNumber / TB_IN_BYTES > 0) {
    return df.format((double) byteNumber / (double) TB_IN_BYTES) + "TB";
} else if (byteNumber / GB_IN_BYTES > 0) {
    return df.format((double) byteNumber / (double) GB_IN_BYTES) + "GB";
} else if (byteNumber / MB_IN_BYTES > 0) {
    return df.format((double) byteNumber / (double) MB_IN_BYTES) + "MB";
} else if (byteNumber / KB_IN_BYTES > 0) {
    return df.format((double) byteNumber / (double) KB_IN_BYTES) + "KB";
...
StringtoHuman(long amount)
to Human
return humanReadableByteCount(amount, true);
StringtoHumanReadableSize(long byteCount)
to Human Readable Size
if (byteCount >= 10 * 1024 * 1024) {
    return (byteCount / (1024 * 1024)) + "MB";
} else if (byteCount >= 10 * 1024) {
    return (byteCount / 1024) + "kB";
} else {
    return Long.toString(byteCount);