代码是这个样子的:
String[] formats = {
"%1$-9d",
"%1$#5X",
"%1$+9d",
"%1$09d",
"%1$,9d",
"%1$(9d"
};
//java中没有foreach关键字,但可以用for (type varname : iterable)实现foreach
for(String format : formats)
{
int num = -9999999;
String formated = String.format(format,num);
System.out.println(formated);
String output = String.format("String.format(\"%1s\",%2$d) == \"%3s\"",
format,
num,
formated
) ;
System.out.println(output);
}
输出如下:
-9999999
String.format("%1$-9d",-9999999) == "-9999999"
0XFF676981
String.format("%1$#5X",-9999999) == "-9999999"
-9999999
String.format("%1$+9d",-9999999) == "-9999999"
-09999999
String.format("%1$09d",-9999999) == "-9999999"
-9,999,999
String.format("%1$,9d",-9999999) == "-9999999"
(9999999)
String.format("%1$(9d",-9999999) == "-9999999"
可以看到formated是正确格式化的结果,而最后的完整输出中==后面的字符串是错误的,为什么?谢谢
慕的地6264312
相关分类