我在正确格式化时遇到问题:
starting mechanical cuchoo clock time [ 0:00:00], total drift = 0.00 seconds
after 1 day mechanical cuchoo clock time [23:59:00], total drift = 60.00 seconds
正确的格式是:
starting mechanical cuckoo clock time [ 0:00:00], total drift = 0.00 seconds
after 1 day mechanical cuckoo clock time [23:59:00], total drift = 60.00 seconds
我试过这个,它确实有效,但有更好的方法吗?
System.out.printf("%60s", this.getClockType() + " cuchoo clock time [" + time.formattedReportedTime() +
"], " + "total drift = ");
System.out.printf("%s", fmt.format(time.getTotalDrift()) + "n");
除了Caio的答案之外,还有一个小片段。
String format =
"%12s mechanical cuckoo clock time [%8s], total drift = %5.2f seconds%n";
System.out.printf(format, "starting", "0:00:00", 0.0);
System.out.printf(format, "after 1 day", "23:59:00", 60.0);
输出
starting mechanical cuckoo clock time [ 0:00:00], total drift = 0.00 seconds
after 1 day mechanical cuckoo clock time [23:59:00], total drift = 60.00 seconds
-
%12s
- (第一个)参数将被格式化为左侧填充字符串,宽度为 12 个字符 -
%8s
- 如上所述,包含 8 个字符的 (第二个) 参数 -
%5.2f
- (第三个)参数(必须是浮点类型)的格式为 5 个字符,精度为 2
您应该查看此链接。这是格式化程序类文档,它非常有用!