Here you can find the source of intToStringWithZeroFill(int intValue, int width)
Parameter | Description |
---|---|
intValue | The integer to convert. |
width | Width of result field. |
public static String intToStringWithZeroFill(int intValue, int width)
//package com.java2s; /* Please see the license information at the end of this file. */ public class Main { /** Convert integer to string with left zero fill. */*from w w w . ja v a 2s . c om*/ * @param intValue The integer to convert. * @param width Width of result field. * * @return The integer converted to a string * with enough leading zeros to fill * the specified field width. */ public static String intToStringWithZeroFill(int intValue, int width) { String s = new Integer(intValue).toString(); if (s.length() < width) s = dupl('0', width - s.length()) + s; return s; } /** Duplicate character into string. * * @param ch The character to be duplicated. * @param n The number of duplicates desired. * * @return String containing "n" copies of "ch". * * <p> * if n <= 0, the empty string "" is returned. * </p> */ public static String dupl(char ch, int n) { if (n > 0) { StringBuffer result = new StringBuffer(n); for (int i = 0; i < n; i++) { result.append(ch); } return result.toString(); } else { return ""; } } /** Duplicate string into string. * * @param s The string to be duplicated. * @param n The number of duplicates desired. * * @return String containing "n" copies of "s". * * <p> * if n <= 0, the empty string "" is returned. * </p> */ public static String dupl(String s, int n) { if (n > 0) { StringBuffer result = new StringBuffer(n); for (int i = 0; i < n; i++) { result.append(s); } return result.toString(); } else { return ""; } } }