3

I am doing some sample practice for formatting string using format() and printf() methods. Here is an example, whose output i am confused about please help.

int i2= 12345;
//Total length of the argument should be 7 and pad with zeros
System.out.printf(">%0,7d< \N",i2);

The output is

>012,345<

If we see, the length of the argument is six. But does the delimiter is also counted in its length. If not then the output should have been this according to me.

>001,2345<

Please help in clarifying this doubt.

4

3 回答 3

3

javadoc 页面Formatter在解释宽度时没有提到对分组分隔符的任何特殊处理,它只是讨论了字符数:

可选宽度是一个非负十进制整数,指示要写入输出的最小字符数。

于 2013-07-23T17:39:24.547 回答
1

您使用了逗号并打印了一个整数,这意味着“7”是一个宽度而不是一个精度。宽度值是指整个格式化字符串,包括前导零、空格、逗号、小数和负号。

于 2013-07-23T17:40:11.603 回答
0

请参阅IO 格式化帮助页面。基本结构如下(点击查看)。

根据此文档:

宽度表示格式化值的最小宽度;必要时填充该值。默认情况下,该值左填充空白。

于 2013-07-23T17:51:00.277 回答