Java Integer Pad Zero zeroPadInteger(int i)

Here you can find the source of zeroPadInteger(int i)

Description

zero Pad Integer

License

Apache License

Parameter

Parameter Description
i Integer to add prefix of zeros too. If passed 2005, will return the String <code>0000002005</code>. String width is the width of Integer.MAX_VALUE as a string (10 digits).

Return

Padded String version of i.

Declaration

public static String zeroPadInteger(int i) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w  w .j ava 2 s .c  om*/
     * Default character to use padding strings.
     */
    private static final char DEFAULT_PAD_CHAR = ' ';
    public static int MAX_INT_CHAR_WIDTH = Integer.toString(Integer.MAX_VALUE).length();

    /**
     * @param i Integer to add prefix of zeros too.  If passed
     * 2005, will return the String <code>0000002005</code>. String
     * width is the width of Integer.MAX_VALUE as a string (10
     * digits).
     * @return Padded String version of <code>i</code>.
     */
    public static String zeroPadInteger(int i) {
        return padTo(Integer.toString(i), MAX_INT_CHAR_WIDTH, '0');
    }

    /** 
     * Convert an <code>int</code> to a <code>String</code>, and pad it to
     * <code>pad</code> spaces.
     * @param i the int
     * @param pad the width to pad to.
     * @return String w/ padding.
     */
    public static String padTo(final int i, final int pad) {
        String n = Integer.toString(i);
        return padTo(n, pad);
    }

    /** 
     * Pad the given <code>String</code> to <code>pad</code> characters wide
     * by pre-pending spaces.  <code>s</code> should not be <code>null</code>.
     * If <code>s</code> is already wider than <code>pad</code> no change is
     * done.
     *
     * @param s the String to pad
     * @param pad the width to pad to.
     * @return String w/ padding.
     */
    public static String padTo(final String s, final int pad) {
        return padTo(s, pad, DEFAULT_PAD_CHAR);
    }

    /** 
     * Pad the given <code>String</code> to <code>pad</code> characters wide
     * by pre-pending <code>padChar</code>.
     * 
     * <code>s</code> should not be <code>null</code>. If <code>s</code> is
     * already wider than <code>pad</code> no change is done.
     *
     * @param s the String to pad
     * @param pad the width to pad to.
     * @param padChar The pad character to use.
     * @return String w/ padding.
     */
    public static String padTo(final String s, final int pad, final char padChar) {
        String result = s;
        int l = s.length();
        if (l < pad) {
            StringBuffer sb = new StringBuffer(pad);
            while (l < pad) {
                sb.append(padChar);
                l++;
            }
            sb.append(s);
            result = sb.toString();
        }
        return result;
    }
}

Related

  1. zeroPad(int value, int padding)
  2. zeroPadArray(byte[] source, int size)
  3. ZeroPadded(Integer i)
  4. zeroPaddedHex(int n, int length)
  5. zeroPaddedInt(int num, int length)