Java Integer Format format(int i, int fillLength)

Here you can find the source of format(int i, int fillLength)

Description

format

License

Open Source License

Declaration

public static String format(int i, int fillLength) 

Method Source Code

//package com.java2s;

public class Main {
    public static final int ALIGN_RIGHT = 0;
    public static final int ALIGN_LEFT = 1;
    private static final char defaultSplitChar = ' ';

    public static String format(String s, int fillLength) {
        return format(s, fillLength, defaultSplitChar, ALIGN_LEFT);
    }/*from w  ww. j a v  a  2  s .co m*/

    public static String format(int i, int fillLength) {
        return format(Integer.toString(i), fillLength, defaultSplitChar, ALIGN_RIGHT);
    }

    public static String format(long l, int fillLength) {
        return format(Long.toString(l), fillLength, defaultSplitChar, ALIGN_RIGHT);
    }

    public static String format(String s, int fillLength, char fillChar, int align) {
        if (s == null) {
            s = "";
        } else {
            s = s.trim();
        }
        int charLen = fillLength - s.length();
        if (charLen > 0) {
            char[] fills = new char[charLen];
            for (int i = 0; i < charLen; i++) {
                fills[i] = fillChar;
            }
            StringBuilder str = new StringBuilder(s);
            switch (align) {
            case ALIGN_RIGHT:
                str.insert(0, fills);
                break;
            case ALIGN_LEFT:
                str.append(fills);
                break;
            default:
                str.append(fills);
            }
            return str.toString();
        } else {
            return s;
        }
    }
}

Related

  1. convertInt2Percent(int num)
  2. format(int c)
  3. format(int color)
  4. format(int i, int length, boolean left_justify, char fill)
  5. format(int intval)
  6. format(int num)
  7. format(int num, int length)