Java String Pad Left leftPad(final String numStr, final int numDigits)

Here you can find the source of leftPad(final String numStr, final int numDigits)

Description

Left pad given number string with zeros.

License

BSD License

Parameter

Parameter Description
numStr String format number
numDigits Number of digits field should be

Return

Left padded numeric string

Declaration

private static String leftPad(final String numStr, final int numDigits) 

Method Source Code

//package com.java2s;
/*L/*from  w  w  w  .  j ava2s.  com*/
 *  Copyright RTI International
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/webgenome/LICENSE.txt for details.
 */

public class Main {
    /**
     * Left pad given number string with zeros.
     * @param numStr String format number
     * @param numDigits Number of digits field should be
     * @return Left padded numeric string
     */
    private static String leftPad(final String numStr, final int numDigits) {
        StringBuffer buff = new StringBuffer(numStr);
        int delta = numDigits - numStr.length();
        for (int i = 0; i < delta; i++) {
            buff.insert(0, '0');
        }
        return buff.toString();
    }
}

Related

  1. leftAlignedPaddedString(String input, int length)
  2. leftPad(byte[] data, int size, byte pad)
  3. leftPad(final String input, final int size)
  4. leftPad(final String str, final int size, String padStr)
  5. leftPad(final String value, final int length, final char paddingChar)
  6. leftPad(long value, char padChar, int maxDigits, StringBuilder buf)
  7. leftPad(Object obj, char pad, int len)