yukaiZhao
玉开
2012-05-11 09:49

java格式化问题

  • java

代码是这个样子的:
[code="java"]
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);
}
[/code]
输出如下:
-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是正确格式化的结果,而最后的完整输出中==后面的字符串是错误的,为什么?谢谢

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换