Java String Leading Character addLeadingZeros(String number, int maxDigits)

Here you can find the source of addLeadingZeros(String number, int maxDigits)

Description

adds some leading '0' to the sting until the length maxDigits is reached

License

Open Source License

Parameter

Parameter Description
number the number to convert
maxDigits the amount of digits expected

Return

the expected number

Declaration

public static String addLeadingZeros(String number, int maxDigits) 

Method Source Code

//package com.java2s;
/***************************************************************************
 *                   (C) Copyright 2003-2011 - Marauroa                    *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

public class Main {
    /**//www. j av a 2  s . co  m
     * adds some leading '0' to the sting until the length <i>maxDigits</i> is
     * reached
     * 
     * @param number
     *            the number to convert
     * @param maxDigits
     *            the amount of digits expected
     * @return the expected number
     */
    public static String addLeadingZeros(String number, int maxDigits) {
        StringBuilder result = new StringBuilder(number);

        while (result.length() < maxDigits) {
            result.insert(0, "0");
        }

        return result.toString();
    }
}

Related

  1. addLeadingZero(final int value)
  2. addLeadingZero(String aString, int aAmountOfZeros)
  3. addLeadingZeroesTo16BitBinaryString(String binaryValue)
  4. addLeadingZeros(Object number, int places)
  5. addLeadingZeros(String deweyCallNum)
  6. addLeadingZeros(String s, int nZeros)
  7. addLeadingZeros(String str)
  8. addLeadingZerosToNumber(long number, int length)