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

StringhumanReadableByteCount(Long bytes, boolean decimal)
Takes the given bytes and transforms them into a human readable format
if (bytes == null)
    bytes = 0L;
String sign = bytes < 0 ? "-" : "";
Long absBytes = Math.abs(bytes);
int unit = decimal ? 1000 : 1024;
if (absBytes < unit) {
    return sign + absBytes + " B";
int exp = (int) (Math.log(absBytes) / Math.log(unit));
String pre = (decimal ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (decimal ? "" : "i");
return String.format("%s %.2f %sB", sign, absBytes / Math.pow(unit, exp), pre).trim();
StringhumanReadableByteCount(long bytes, boolean si)
human Readable Byte Count
int unit = 1024;
if (bytes < unit)
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = "KMGTPE".charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %s", bytes / Math.pow(unit, exp), pre);
StringhumanReadableByteCount(long bytes, boolean si)
PURPOSE:
humanReadableByteCount

int unit = si ? 1000 : 1024;
if (bytes < unit)
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
StringhumanReadableByteCount(long bytes, boolean si)
human Readable Byte Count
int unit = si ? 1000 : 1024;
if (bytes < unit) {
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
StringhumanReadableBytes(long bytes)
Get bytes in human readable form.
int unit = 1024;
if (bytes < unit) {
    return bytes + " B";
} else {
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), "KMGTPE".charAt(exp - 1));
StringhumanReadableBytes(long bytes)
Format the provided integer parameter in units of bytes as a human-readable string.
return humanReadableBytes(bytes, false, true);
StringhumanReadableDateDiff(long start, long end)
human Readable Date Diff
long diff = end - start;
StringBuilder sb = new StringBuilder();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * 24 * 60 * 60 * 1000;
if (days >= 1) {
    sb.append(days);
    sb.append("d");
long hours = diff / (60 * 60 * 1000);
diff -= hours * 60 * 60 * 1000;
if (hours >= 1) {
    if (sb.length() > 1) {
        sb.append(' ');
    sb.append(hours);
    sb.append("h");
if (sb.length() > 1) {
    sb.append(' ');
long minutes = diff / (60 * 1000);
diff -= minutes * 60 * 1000;
sb.append(minutes);
sb.append("m ");
sb.append(Math.round(Math.ceil(diff / 1000.0)));
sb.append("s");
return sb.toString();
StringhumanReadableDuration(long length)
human Readable Duration
long minutes = length / 60;
long seconds = length - (minutes * 60);
StringBuilder builder = new StringBuilder();
if (minutes != 0) {
    builder.append(minutes).append(" minute");
    if (minutes != 1) {
        builder.append("s");
if (seconds != 0 || minutes == 0) {
    if (builder.length() > 0) {
        builder.append(", ");
    builder.append(seconds).append(" second");
    if (seconds != 1) {
        builder.append("s");
return builder.toString();
StringhumanReadableDuration(long ms)
Convert milliseconds to a human readable duration
final StringBuilder sb = new StringBuilder();
if (ms < 1999) { 
    sb.append("1 second");
} else if (ms < 60 * 1000) { 
    sb.append(ms / 1000);
    sb.append(" seconds");
} else if (ms < 60 * 60 * 1000) { 
    final long minutes = ms / (60 * 1000);
...
StringhumanReadableInt(long number)
Given an integer, return a string that is in an approximate, but human readable format.
long absNumber = Math.abs(number);
double result = number;
String suffix = "";
if (absNumber < 1024) {
} else if (absNumber < 1024 * 1024) {
    result = number / 1024.0;
    suffix = "k";
} else if (absNumber < 1024 * 1024 * 1024) {
...