使用 system.out.printf 格式化 java 字符串 [英] Formatting java strings with system.out.printf

查看:111
本文介绍了使用 system.out.printf 格式化 java 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找很多关于 java 中用于格式化字符串输出的 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.

我正在尝试打印看起来像这样的漂亮列

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);

我认为-1s"是左对齐的减号,1 将是第一个和第二个输入之间的空格,然后 %10s 将相隔十个字符,%10.2 将相隔十个字符并保留 2 个小数位.所以我试着做

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. 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.

修改尝试使用

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

结果...

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 表示在字段中左对齐至少一个字符宽.但是您的第一列始终至少有 3 个字符宽,因此会忽略说明符.纯粹出于巧合,第一列看起来很整齐:您的所有数字碰巧都是三位数.

%-1 means left align in a field at least one character wide. But your first column is always at least 3 characters wide, so the specifier is ignored. The first column comes out looking neat purely by coincidence: all your numbers happen to have three digits.

同样,%10 表示在至少 10 个字符宽的字段中右对齐.但是您所有的相机名称都比这长得多,因此实际上忽略了说明符.

Similarly, %10 means right align in a field at least 10 characters wide. 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

这意味着:

  1. 左对齐字段中的第一个参数,宽度至少为 4 个字符
  2. 在至少 25 个字符宽的字段中左对齐第二个参数
  3. 在至少 6 个字符宽的字段中打印右对齐的数字,小数点后保留两位数

您可以将 %4s 替换为 %3s 以获得类似的效果.不同之处在于溢出的处理方式.第一个版本在第一列中长度超过 3 个字符的值后面没有空格.第二个会.

You could replace %4s with %3s<space> for a similar effect. 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. The second one would.

这篇关于使用 system.out.printf 格式化 java 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆