Java Integer Pad Zero zeroPad(int n, int base, int width)

Here you can find the source of zeroPad(int n, int base, int width)

Description

Zero-pad an integer.

License

Open Source License

Parameter

Parameter Description
n The integer.
base The base, either 10 or 16. If 16, any values 10-15 are returned as upper-case A-F.
width The field width, maximum 8.

Return

Returns a string representation of the integer zero-padded to the given width.

Declaration

public static String zeroPad(int n, int base, int width) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  w  w .  ja va2 s.  c  o  m*/
     * Zero-pad an integer.
     *
     * @param n The integer.
     * @param base The base, either 10 or 16.  If 16, any values 10-15 are
     * returned as upper-case A-F.
     * @param width The field width, maximum 8.
     * @return Returns a string representation of the integer zero-padded to
     * the given width.
     */
    public static String zeroPad(int n, int base, int width) {
        final String s;
        switch (base) {
        case 10:
            s = Integer.toString(n);
            break;
        case 16:
            s = Integer.toHexString(n).toUpperCase();
            break;
        default:
            throw new IllegalArgumentException("base must be 10 or 16");
        }
        final int len = s.length();
        return len < width ? "0000000".substring(7 - width + len) + s : s;
    }
}

Related

  1. zeroPad(final byte[] data, final int blockSize)
  2. zeroPad(final int amount, final String in)
  3. zeroPad(final int length, final byte[] bytes)
  4. zeropad(final int num, final int size)
  5. zeroPad(int i, int len)
  6. zeroPad(int n, int len)
  7. zeroPad(int number, int places)
  8. ZeroPad(int number, int width)
  9. zeroPad(int value, int padding)