本文翻译自:Add leading zeroes to number in Java? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How can I pad an integer with zeros on the left? 如何在左侧填充带零的整数? 14 answers 14个答案
Is there a better way of getting this result? 有没有更好的方法来获得这个结果? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something) 如果num的位数多于数字,则此函数失败,我觉得它应该在某个库中(如Integer.toString(x,“%3d”)或其他东西)
static String intToString(int num, int digits) {
StringBuffer s = new StringBuffer(digits);
int zeroes = digits - (int) (Math.log(num) / Math.log(10)) - 1;
for (int i = 0; i < zeroes; i++) {
s.append(0);
}
return s.append(num).toString();
}
#1楼
参考:https://stackoom.com/question/19ix/在Java中将前导零添加到数字-重复
#2楼
String.format ( http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax ) String.format( http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax )
In your case it will be: 在你的情况下,它将是:
String formatted = String.format("%03d", num);
- 0 - to pad with zeros 0 - 用零填充
- 3 - to set width to 3 3 - 将宽度设置为3
#3楼
Since Java 1.5 you can use the String.format
method. 从Java 1.5开始,您可以使用String.format
方法。 For example, to do the same thing as your example: 例如,要做与您的示例相同的事情:
String format = String.format("%%0%dd", digits);
String result = String.format(format, num);
return result;
In this case, you're creating the format string using the width specified in digits, then applying it directly to the number. 在这种情况下,您将使用数字指定的宽度创建格式字符串,然后将其直接应用于数字。 The format for this example is converted as follows: 此示例的格式转换如下:
%% --> %
0 --> 0
%d --> <value of digits>
d --> d
So if digits is equal to 5, the format string becomes %05d
which specifies an integer with a width of 5 printing leading zeroes. 因此,如果数字等于5,则格式字符串变为%05d
,其指定宽度为5的整数打印前导零。 See the java docs for String.format
for more information on the conversion specifiers. 有关转换说明符的更多信息,请参阅String.format
文档的String.format
文档 。
#4楼
Another option is to use DecimalFormat to format your numeric String. 另一种选择是使用DecimalFormat格式化数字String。 Here is one other way to do the job without having to use String.format if you are stuck in the pre 1.5 world: 如果你被困在pre 1.5世界中,这里有另一种方法来完成这项工作,而不必使用String.format:
static String intToString(int num, int digits) {
assert digits > 0 : "Invalid number of digits";
// create variable length array of zeros
char[] zeros = new char[digits];
Arrays.fill(zeros, '0');
// format number as String
DecimalFormat df = new DecimalFormat(String.valueOf(zeros));
return df.format(num);
}
#5楼
In case of your jdk version less than 1.5, following option can be used. 如果您的jdk版本低于1.5,可以使用以下选项。
int iTest = 2;
StringBuffer sTest = new StringBuffer("000000"); //if the string size is 6
sTest.append(String.valueOf(iTest));
System.out.println(sTest.substring(sTest.length()-6, sTest.length()));
#6楼
How about just: 怎么样:
public static String intToString(int num, int digits) {
String output = Integer.toString(num);
while (output.length() < digits) output = "0" + output;
return output;
}