Here you can find the source of addLeadingZeros(String number, int maxDigits)
Parameter | Description |
---|---|
number | the number to convert |
maxDigits | the amount of digits expected |
public static String addLeadingZeros(String number, int maxDigits)
//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(); } }