Android Long Format toZeroPaddedString(long value, int precision, int maxSize)

Here you can find the source of toZeroPaddedString(long value, int precision, int maxSize)

Description

If necessary, adds zeros to the beginning of a value so that the total length matches the given precision, otherwise trims the right digits.

License

Open Source License

Declaration

public static String toZeroPaddedString(long value, int precision,
        int maxSize) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w w  w  .ja v  a 2 s.  c  om
     * If necessary, adds zeros to the beginning of a value so that the total
     * length matches the given precision, otherwise trims the right digits.
     * Then if maxSize is smaller than precision, trims the right digits to
     * maxSize. Negative values are treated as positive
     */
    public static String toZeroPaddedString(long value, int precision,
            int maxSize) {

        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();
    }
}

Related

  1. splitLongs(String str)
  2. convert2Percent(long readsize, long totalsize)
  3. formatSize(long value)
  4. formatSpaceSize(long file_size)