Reproduced from: http://ych0108.iteye.com/blog/2174134 java in the int to the number of string digits not up front 0
0 represents the character to be filled before
10 represents string length
D indicates that the parameter is an integer type
Today I would like to turn int to string digits not enough ahead of 0, in the original wanted to see if there is no ready-made API, the results of the search out most of the following this
public static string Addzerofornum (String Str,int strlength) {
int strLen =str.length ();
if (StrLen <strlength) {while
(strlen< strlength) {
STRINGBUFFERSB = new StringBuffer ();
Sb.append ("0"). Append (str);//Left 0
// sb.append (str). Append ("0");/right complement 0
str= sb.tostring ();
Strlen= str.length ();
}
return str;
}
But I feel a little bit of trouble, I think to think of a slightly simpler way, such as the next line can
String str = String.Format ("%5d", num). Replace ("", "0");
where num is int, str is the result of the conversion. It's simple.
Recently I searched again about the string.format thing, actually has the own complement 0 method,
String.Format ("%06", 12);//0 means to fill 0 instead of space, 6 means at least 6 digits