Android Utililty Methods Long Format

List of utility methods to do Long Format

Description

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

Method

StringtoZeroPaddedString(long value, int precision, int maxSize)
If necessary, adds zeros to the beginning of a value so that the total length matches the given precision, otherwise trims the right digits.
StringBuffer sb = new StringBuffer();
if (value < 0) {
    value = -value;
String s = Long.toString(value);
if (s.length() > precision) {
    s = s.substring(precision);
for (int i = s.length(); i < precision; i++) {
    sb.append('0');
sb.append(s);
if (maxSize < precision) {
    sb.setLength(maxSize);
return sb.toString();
long[]splitLongs(String str)
Parse comma-separated list of longs and return as array.
StringTokenizer tokenizer = new StringTokenizer(str, ",");
int n = tokenizer.countTokens();
long[] list = new long[n];
for (int i = 0; i < n; i++) {
    String token = tokenizer.nextToken();
    list[i] = Long.parseLong(token);
return list;
...
Stringconvert2Percent(long readsize, long totalsize)
convert Percent
String str = "00.00%";
float result = ((float) readsize) / (float) totalsize;
System.out.println(result);
DecimalFormat df = new DecimalFormat("##.##%");
str = df.format(result);
return str;
StringformatSize(long value)
format Size
double k = (double) value / 1024;
if (k == 0) {
    return String.format("%dB", value);
double m = k / 1024;
if (m < 1) {
    return String.format("%.1fK", k);
double g = m / 1024;
if (g < 1) {
    return String.format("%.1fM", m);
return String.format("%.1fG", g);
StringformatSpaceSize(long file_size)
format Space Size
if (file_size < 1024) {
    return file_size + "B";
} else if (file_size < 1024 * 1024) {
    return NO_DECIMAL_POINT_DF.format((double) file_size / 1024)
            + "KB";
} else if (file_size < 1024 * 1024 * 1024) {
    return NO_DECIMAL_POINT_DF
            .format((double) file_size / 1024 / 1024) + "MB";
...
StringformatPercent(long divisor, long dividend)
format Percent
if (dividend == 0) {
    return null;
double ret = (double) (divisor * 100) / dividend;
return String.format(Locale.CHINA, "%.2f%%", ret);
StringformatSize(long value)
format Size
double k = (double) value / 1024;
if (k == 0) {
    return String.format(Locale.CHINA, "%d B", value);
double m = k / 1024;
if (m < 1) {
    return String.format(Locale.CHINA, "%.2f K", k);
double g = m / 1024;
if (g < 1) {
    return String.format(Locale.CHINA, "%.2f M", m);
return String.format(Locale.CHINA, "%.2f G", g);
StringgetTimeFormattedFromMillis(long millis)
Returns a formatted time string in the form of (hh:)mm:ss generated from milliseconds
long duration = millis / 1000;
long h = duration / 3600;
long m = (duration - h * 3600) / 60;
long s = duration - (h * 3600 + m * 60);
String durationString = "";
if (h != 0) {
    durationString += StringUtil.getZeroPaddedString(h) + ":";
durationString += StringUtil.getZeroPaddedString(m) + ":";
durationString += StringUtil.getZeroPaddedString(s);
return durationString;
StringformatFileSize(long length)
format File Size
String result = null;
int sub_string = 0;
if (length >= 1073741824) {
    sub_string = String.valueOf((float) length / 1073741824)
            .indexOf(".");
    result = ((float) length / 1073741824 + "000").substring(0,
            sub_string + (length % 1073741824 > 0 ? 3 : 0)) + "G";
} else if (length >= 1048576) {
...
StringReadableByteCount(long bytes)
Readable Byte Count
int unit = 1024;
if (bytes < unit)
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = String.valueOf("KMGTPE".charAt(exp - 1));
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);