int di = 1; //这里假设值为1 也可以是从其他地方传过来的值 String sr = ""+di; for(int i=sr.length;i<4;i++){ //只要没到4位就前加"0" 也可以换成任意N位 sr = "0"+sr; } System.out.println(sr);
/** * 以指定的字符左填充 * * @param s 需要填充的字串 * @param len 填充后的长度 * @param c 指定的填充字符 * @return 左填充零后的字串 */ public static String padLeft(String s, int len, char c) { if (s == null) { s = ""; } s = s.trim(); if (length(s) >= len) { return s; } StringBuffer d = new StringBuffer(len); int fill = len - length(s); while (fill-- > 0) { d.append(c); } d.append(s); return d.toString(); }
public static String change(int di){ //参数为要转换的数字 String sr = ""+di; for(int i=sr.length;i<4;i++){ //只要没到4位就前加"0" 也可以换成任意N位 sr = "0"+sr; } return sr; } 调用这个方法就好了
可以试试apache的commons-lang包org.apache.commons.lang Class StringUtils java.lang.Object | +--org.apache.commons.lang.StringUtilsIsEmpty/IsBlank - checks if a String contains text Trim/Strip - removes leading and trailing whitespace Equals - compares two strings null-safe IndexOf/LastIndexOf/Contains - null-safe index-of checks IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings ContainsOnly/ContainsNone - does String contains only/none of these characters Substring/Left/Right/Mid - null-safe substring extractions SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings Split/Join - splits a String into an array of substrings and vice versa Remove/Delete - removes part of a String Replace/Overlay - Searches a String and replaces one String with another Chomp/Chop - removes the last part of a String LeftPad/RightPad/Center/Repeat - pads a String UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String CountMatches - counts the number of occurrences of one String in another IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String DefaultString - protects against a null input String Reverse/ReverseDelimited - reverses a String Abbreviate - abbreviates a string using ellipsis Difference - compares two Strings and reports on their differences LevensteinDistance - the number of changes needed to change one String into another
int i = 1; NumberFormat df = NumberFormat.getInstance(); df.setMinimumIntegerDigits(3);
System.out.println(df.format(i));
imA(男的不会,会的不男) ( ) 信誉:3 Blog 加为好友 2007-4-24 15:37:36 得分:
用DecimalFormat格式化就可以了。public class Test { public static void main(String[] args){ java.text.DecimalFormat df=new java.text.DecimalFormat("000"); for(int i=0;i<10;i++) System.out.println(df.format(i)); } } -------------------------------------
厉害
收藏向CSDN的高手们致以节日问候!
public class TTest { public static void main (String[] args) { System.out.println(addLeadingZero(7, 1)); // 7 System.out.println(addLeadingZero(7, 3)); // 007 System.out.println(addLeadingZero(17, 3)); // 017 }
private static String addLeadingZero (int $n, int $d) { String t = "";
if(Integer.toString(value).length==1)
return "00"+Integer.toString(value);
else if(Integer.toString(value).length==2)
return "0"+Integer.toString(value);
else
return Integer.toString(value);
}
String sr = ""+di;
for(int i=sr.length;i<4;i++){ //只要没到4位就前加"0" 也可以换成任意N位
sr = "0"+sr;
}
System.out.println(sr);
* 以指定的字符左填充
*
* @param s 需要填充的字串
* @param len 填充后的长度
* @param c 指定的填充字符
* @return 左填充零后的字串
*/
public static String padLeft(String s, int len, char c) { if (s == null) {
s = "";
}
s = s.trim();
if (length(s) >= len) {
return s;
} StringBuffer d = new StringBuffer(len);
int fill = len - length(s); while (fill-- > 0) {
d.append(c);
} d.append(s);
return d.toString();
}
String sr = ""+di;
for(int i=sr.length;i<4;i++){ //只要没到4位就前加"0" 也可以换成任意N位
sr = "0"+sr;
}
return sr;
}
调用这个方法就好了
{
if(i < 10)
return "00" + Integer.toString(i);
else if(i < 100)
return "0" + Integer.toString(i);
else
return Integer.toString(i);
}
public static void main(String[] args){
java.text.DecimalFormat df=new java.text.DecimalFormat("000");
for(int i=0;i<10;i++)
System.out.println(df.format(i));
}
}
format.setMinimumIntegerDigits(3);
public String format(int i)
{
return format.format(i);
}
public static String change(int di){ //参数为要转换的数字
String sr = ""+di;
StringBuffer tempsr = new StringBuffer(sr);
for(int i=tempsr.length();i<4;i++){ //只要没到4位就前加"0" 也可以换成任意N位
tempsr.insert(0, "0");
}
return tempsr.toString();
}
public static String change(int di,int bt){
String sr = ""+di;
StringBuffer tempsr = new StringBuffer(sr);
for(int i=tempsr.length();i<bt;i++){ //只要没到4位就前加"0" 也可以换成任意N位
tempsr.insert(0, "0");
}
return tempsr.toString();
}
public static void main(String[] args) {
System.out.println(change(10,4));//第一个参数为要转换的数字 第二个参数为需要的位数
}
}
还要找API不成?
难道你什么都自己写?幼稚!
public String getStringInt(int n){
String tmp = ""+n;
while(tmp.length()<3){
tmp = "0" + tmp;
}
return tmp;
}
方法2,用DecimalFormat
public String getStringInt(int n){
DecimalFormat df = new DecimalFormat("000");
return df.format(n);
}
Class StringUtils
java.lang.Object
|
+--org.apache.commons.lang.StringUtilsIsEmpty/IsBlank - checks if a String contains text
Trim/Strip - removes leading and trailing whitespace
Equals - compares two strings null-safe
IndexOf/LastIndexOf/Contains - null-safe index-of checks
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
ContainsOnly/ContainsNone - does String contains only/none of these characters
Substring/Left/Right/Mid - null-safe substring extractions
SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
Split/Join - splits a String into an array of substrings and vice versa
Remove/Delete - removes part of a String
Replace/Overlay - Searches a String and replaces one String with another
Chomp/Chop - removes the last part of a String
LeftPad/RightPad/Center/Repeat - pads a String
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
CountMatches - counts the number of occurrences of one String in another
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
DefaultString - protects against a null input String
Reverse/ReverseDelimited - reverses a String
Abbreviate - abbreviates a string using ellipsis
Difference - compares two Strings and reports on their differences
LevensteinDistance - the number of changes needed to change one String into another
NumberFormat df = NumberFormat.getInstance();
df.setMinimumIntegerDigits(3);
System.out.println(df.format(i));
用DecimalFormat格式化就可以了。public class Test {
public static void main(String[] args){
java.text.DecimalFormat df=new java.text.DecimalFormat("000");
for(int i=0;i<10;i++)
System.out.println(df.format(i));
}
}
-------------------------------------
厉害
{ public static void main (String[] args)
{
System.out.println(addLeadingZero(7, 1)); // 7
System.out.println(addLeadingZero(7, 3)); // 007
System.out.println(addLeadingZero(17, 3)); // 017
}
private static String addLeadingZero (int $n, int $d)
{
String t = "";
int i = (int)Math.log10($n) + 1;
if ((i = ($d - i)) > 0) while (i-- > 0) t += '0';
return t + $n;
}
}