Here you can find the source of zeroPad(int i, int len)
Parameter | Description |
---|---|
i | The number to pad |
len | The length required |
public static String zeroPad(int i, int len)
//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; } } }