Java Integer Pad Zero zeroPad(int i, int len)

Here you can find the source of zeroPad(int i, int len)

Description

Zero Pad an int

License

Open Source License

Parameter

Parameter Description
i The number to pad
len The length required

Return

The padded number

Declaration

public static String zeroPad(int i, int len) 

Method Source Code

//package com.java2s;
/*/*w w w. jav  a  2s .c  om*/
 *  This file is part of the Alchemy project - http://al.chemy.org
 * 
 *  Copyright (c) 2007-2010 Karl D.D. Willis
 * 
 *  Alchemy is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Alchemy is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with Alchemy.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    /** Zero Pad an int 
     * 
     * @param i     The number to pad
     * @param len   The length required
     * @return      The padded number
     */
    public static String zeroPad(int i, int len) {
        // converts integer to left-zero padded string, len chars long.
        String s = Integer.toString(i);
        if (s.length() > len) {
            return s.substring(0, len);
            // pad on left with zeros
        } else if (s.length() < len) {
            return "000000000000000000000000000".substring(0,
                    len - s.length())
                    + s;
        } else {
            return 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 n, int base, int width)
  6. zeroPad(int n, int len)
  7. zeroPad(int number, int places)
  8. ZeroPad(int number, int width)