Here you can find the source of format(int i, int length, boolean left_justify, char fill)
Parameter | Description |
---|---|
i | integer to format |
length | length of desired string |
left_justify | format left or right |
fill | fill character |
public static final String format(int i, int length, boolean left_justify, char fill)
//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; } }