Java Integer Format format(int i, int length, boolean left_justify, char fill)

Here you can find the source of format(int i, int length, boolean left_justify, char fill)

Description

Return a string for an integer justified left or right and filled up with `fill' characters if necessary.

License

Open Source License

Parameter

Parameter Description
i integer to format
length length of desired string
left_justify format left or right
fill fill character

Return

formatted int

Declaration

public static final String format(int i, int length, boolean left_justify, char fill) 

Method Source Code

//package com.java2s;
/*//w  ww  . j  a  va 2  s .c  o  m
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Return a string for an integer justified left or right and filled up with
     * `fill' characters if necessary.
     *
     * @param i integer to format
     * @param length length of desired string
     * @param left_justify format left or right
     * @param fill fill character
     * @return formatted int
     */
    public static final String format(int i, int length, boolean left_justify, char fill) {
        return fillup(Integer.toString(i), length, left_justify, fill);
    }

    /**
     * Fillup char with up to length characters with char `fill' and justify it left or right.
     *
     * @param str string to format
     * @param length length of desired string
     * @param left_justify format left or right
     * @param fill fill character
     * @return formatted string
     */
    public static final String fillup(String str, int length, boolean left_justify, char fill) {
        int len = length - str.length();
        char[] buf = new char[(len < 0) ? 0 : len];

        for (int j = 0; j < buf.length; j++)
            buf[j] = fill;

        if (left_justify)
            return str + new String(buf);
        else
            return new String(buf) + str;
    }
}

Related

  1. convertInt2Percent(int num)
  2. format(int c)
  3. format(int color)
  4. format(int i, int fillLength)
  5. format(int intval)
  6. format(int num)
  7. format(int num, int length)
  8. format(int spaces, String string)