Java Integer Pad Zero zeropad(final int num, final int size)

Here you can find the source of zeropad(final int num, final int size)

Description

Method zeropad

License

Open Source License

Parameter

Parameter Description
num a parameter
size a parameter

Declaration

private static String zeropad(final int num, final int size) 

Method Source Code

//package com.java2s;
/*/*w ww . j  av a  2 s .co m*/
 * Copyright (c) 2006 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

public class Main {
    /**
     * Method zeropad
     *
     * @param num
     * @param size
     */
    private static String zeropad(final int num, final int size) {
        final String value = Integer.toString(num);

        if (value.length() >= size) {
            return value;
        }

        final StringBuffer buf = new StringBuffer(size);
        for (int i = 0; i++ < (size - value.length()); buf.append('0')) {
            ;
        }

        buf.append(value);

        return buf.toString();
    }
}

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(int i, int len)
  5. zeroPad(int n, int base, int width)
  6. zeroPad(int n, int len)
  7. zeroPad(int number, int places)