简体   繁体   English

使用system.out.printf格式化java字符串

[英]Formatting java strings with system.out.printf

I've been looking around at a lot of questions about the System.out.printf in java for formatting string outputs and I just don't seem to understand how to use it. 我一直在寻找关于java中的System.out.printf的许多问题,用于格式化字符串输出,我似乎并不理解如何使用它。

I'm trying to print nice columns that looks like this 我正在尝试打印看起来像这样的漂亮列

601 GoPro Hero5 Black       276.95
602 GoPro Hero5 Session     199.00
611 Canon EOS Rebel         361.89

but the best I can get is this 但我能得到的最好的就是这个

601 GoPro Hero5 Black     276.95
602 GoPro Hero5 Session     199.00
611 Canon EOS Rebel     361.89

I seem to be able to align the first two values but never the third. 我似乎能够对齐前两个值,但从来没有第三个。 Here is my code 这是我的代码

System.out.printf("%-1s %10s %10.2f\n", "ex string", "ex string", 276.95);

I thought the "-1s" was left align for minus, and 1 would be the space between the first and second input, then %10s would be ten characters apart, the %10.2 would be ten apart with 2 decimal places. 我认为“-1s”左对齐为负,1是第一和第二输入之间的空格,然后%10s将相隔10个字符,%10.2将相隔10个小数点后2位。 So I tried to do 所以我试着去做

System.out.printf("%-1s %10s %20.2f\n", "ex string", "ex string", 276.95);

but this still doesn't get those third values inline with one another. 但这仍然没有让那些第三个值相互内联。


Revised attempt using 修改后的尝试使用

System.out.printf("%-10s %-10s %10.2f\n", "ex string", "ex string", 276.95);

results in... 结果是...

601        GoPro Hero5 Black     276.95
602        GoPro Hero5 Session     199.00
611        Canon EOS Rebel     361.89

for the record my code is getting the variables like this: 为了记录我的代码得到这样的变量:

System.out.printf("%-10s %-10s %10.2f\n", items.get(i).getItemNumber(), items.get(i).getName(), items.get(i).getPrice());

The field width specifiers are not spacing specs. 字段宽度说明符不是间距规格。

%-1 means left align in a field at least one character wide. %-1表示在字段中左对齐至少一个字符宽。 But your first column is always at least 3 characters wide, so the specifier is ignored. 但是您的第一列始终至少有3个字符宽,因此忽略了说明符。 The first column comes out looking neat purely by coincidence: all your numbers happen to have three digits. 第一栏看起来整齐纯粹巧合:你的所有数字都碰巧有三位数。

Similarly, %10 means right align in a field at least 10 characters wide. 同样, %10表示在一个至少10个字符宽的字段中右对齐。 But all your camera names are much longer than that, so the specifier is effectively ignored. 但是你的所有摄像机名称都比这长得多,因此有效地忽略了说明符。

You are probably looking for something more like 你可能正在寻找更像的东西

%-4s%-25s%6.2f

This means: 这意味着:

  1. Left align the first argument in a field at least 4 characters wide 左对齐字段中的第一个参数,至少4个字符宽
  2. Left align the second argument in a field at least 25 characters wide 左对齐字段中的第二个参数,宽度至少为25个字符
  3. Print the number right aligned, with two digits after the decimal point, in a field at least 6 characters wide 打印右对齐的数字,小数点后面的两位数字,至少6个字符宽的字段

You could replace %4s with %3s<space> for a similar effect. 您可以使用%3s<space>替换%4s以获得类似的效果。 The difference would be in how overflow is handled. 不同之处在于如何处理溢出。 The first version would not have a space following values in the first column that were longer than 3 characters. 第一个版本的第一列中的值不会超过3个字符。 The second one would. 第二个会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM