Java Utililty Methods Long Number Format

List of utility methods to do Long Number Format

Description

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

Method

Stringcomma(long n)
Copied from com.shopwiki.text.Pretty
return numberFormat.get().format(n);
Stringformat(final long number, final int length)
Trim an integer with leading zeros to a given maximum length.
if (length > FORMATED_ZERO.length()) {
    throw new IllegalArgumentException("maximum length " + FORMATED_ZERO + " exceeded: " + length);
if (length < 1) {
    throw new IllegalArgumentException("length must be bigger than 0: " + length);
final String temp = FORMATED_ZERO + number;
return temp.substring(Math.min(temp.length() - length, FORMATED_ZERO.length()));
...
Stringformat(final long value, final long divider, final String unit)
format
final double result = divider > 1 ? (double) value / (double) divider : (double) value;
return String.format("%.1f %s", result, unit);
Stringformat(final long value, final long divider, final String unit)
Helper method for convertToStringRepresentation(long)
final double result = divider > 1 ? (double) value / (double) divider : (double) value;
return String.format("%.1f %s", Double.valueOf(result), unit);
Stringformat(long bytes)
Depending on the size, return a size formatted in bytes, K, M, G or T bytes.
if (bytes < 0) {
    return "invalid input(" + bytes + ")";
if (bytes < 1.0) {
    return String.format("%d byte", bytes);
} else if (bytes < KILO) {
    return String.format("%d bytes", bytes);
} else if (bytes < MEGA) {
...
Stringformat(long mem)
format
double dMem = mem;
String[] units = new String[] { "", "KB", "MB", "GB" };
for (String unit : units) {
    if (dMem < 1024)
        return String.format("%.1f %s", dMem, unit);
    dMem /= 1024;
return String.format("%.1f %s", dMem * 1024, units[units.length - 1]);
...
Stringformat(long milliseconds)
Only display the hours / minutes / seconds / milliseconds
StringBuilder buff = new StringBuilder();
long elapsedTime = milliseconds / 1000;
if (elapsedTime / 3600 > 0) {
    add(buff, String.format(format, elapsedTime / 3600), "h");
if ((elapsedTime % 3600) / 60 > 0) {
    add(buff, String.format(format, (elapsedTime % 3600) / 60), "m");
if ((elapsedTime % 60) > 0) {
    add(buff, String.format(format, elapsedTime % 60), "s");
if (milliseconds % 1000 > 0) {
    add(buff, String.format(format, milliseconds % 1000), "ms");
return buff.toString();
Stringformat(long ms)
Transform the time in milliseconds to the time in "dd hh:mm:ss.SSS" format.
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
...
Stringformat(long numero, int numeroZeri)
format
return rSet(numero + "", numeroZeri, "0");
Stringformat(long offsetMillis)
quick & dirty format
offsetMillis /= 60 * 1000;
String sign = "+";
if (offsetMillis < 0) {
    offsetMillis = -offsetMillis;
    sign = "-";
return sign + String.valueOf(offsetMillis / 60) + ":"
        + String.valueOf(100 + (offsetMillis % 60)).substring(1, 3);
...