本文将介绍 Java 中的格式化输出涉及的类、方法及其用法。
在 Java 中可使用 System.out.print
或 System.out.println
,将参数输出到控制台。System.out.printf
,可以先将参数进行格式化,然后输出到控制台。
PrintStream 类
如上,System 类的常量 out 是一个 PrintStream 类实例,其输出功能是调用的 PrintStream 的方法。
print 方法和 println 方法
print 用于直接输出参数,比如:
System.out.print("xingchao");
System.out.print("zhang");
// 结果
// xingchaozhang
println 同时在输出末尾,增加换行符,比如:
System.out.println("zhang");
System.out.println("xingchao");
// 结果
// zhang
// xingchao
printf 方法和 format 方法
printf 用于将参数按照指定格式输出,比如:
System.out.printf("This number is %12.2f\n", 123456.123f);
// 结果
// This number is 123456.13
format 方法和 printf 方法一致,因为其实 printf 内部调用的 format。
Formatter 类
Formatter 类是用于格式化参数的解释器,类似于 C 语言的 printf 功能,实现了格式的解析。PrintStream 类的 format 方法,其内部调用就是 Formatter 类的 format 方法。所以掌握 Formatter 类的用法,就掌握了 System.out.printf
。
format 方法
先看例子:
Formatter formatter = new Formatter(System.out);
formatter.format("Num:%12.2f\n", 123456.789f);
// 输出
// Num: 123456.79
简要解释一下:
%12.2f
为格式说明符(format specifier),以 % 开头,解析时用参数 123456.123f
替换。f
为转换符(conversion),表示参数为浮点数,格式化为十进制数字。12
表示输出字符的宽度,这里不足 12 位,开头以空格填充。2
表示小数点后保留 2 位。
格式化功能
我们专门来看下格式化功能。
转换符(conversion)一览表
汇总如下,内容引自文档:Formatter 。
转换符(Conversion) | 参数类别 | 描述 |
---|---|---|
‘b’, ‘B’ | general | If the argument arg is null , then the result is “false “. If arg is a boolean or Boolean , then the result is the string returned by String.valueOf(arg) . Otherwise, the result is “true”. |
‘h’, ‘H’ | general | The result is obtained by invoking Integer.toHexString(arg.hashCode()) . |
‘s’, ‘S’ | general | If arg implements Formattable , then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString() . |
‘c’, ‘C’ | character | The result is a Unicode character |
‘d’ | integral | The result is formatted as a decimal integer |
‘o’ | integral | The result is formatted as an octal integer |
‘x’, ‘X’ | integral | The result is formatted as a hexadecimal integer |
‘e’, ‘E’ | floating point | The result is formatted as a decimal number in computerized scientific notation |
‘f’ | floating point | The result is formatted as a decimal number |
‘g’, ‘G’ | floating point | The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. |
‘a’, ‘A’ | floating point | The result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion is not supported for the BigDecimal type despite the latter’s being in the floating point argument category. |
‘t’, ‘T’ | date/time | Prefix for date and time conversion characters. See Date/Time Conversions. |
‘%’ | percent | The result is a literal '%' ('\u0025' ) |
‘n’ | line separator | The result is the platform-specific line separator |
转换符的用法示例:
System.out.printf("Conversion b: %b, %b, %b\n", true, false, 0);
System.out.printf("Conversion h: %h, %h\n", "chao", "chao");
System.out.printf("Conversion s: %s, %s, %s\n", "chao", 123.45f, false);
System.out.printf("Conversion c: %c, %c\n", '张', 'z');
System.out.printf("Conversion d: %d, %d\n", 12, (int)123.45);
System.out.printf("Conversion o: %o, %o\n", 020, 16);
System.out.printf("Conversion x: %x, %x\n", 0x10, 16);
System.out.printf("Conversion e: %e\n", 123456789.123);
System.out.printf("Conversion f: %f\n", 123456.789);
System.out.printf("Conversion g: %g\n", 123.456);
System.out.printf("Conversion a: %a\n", 123.456);
System.out.printf("Conversion t: %tT\n", Calendar.getInstance());
System.out.printf("Conversion %%: %s\n", "Nothing");
System.out.printf("Conversion n: %s%n", "Just a line");
输出结果:
Conversion b: true, false, true
Conversion h: 2e9353, 2e9353
Conversion s: chao, 123.45, false
Conversion c: 张, z
Conversion d: 12, 123
Conversion o: 20, 20
Conversion x: 10, 10
Conversion e: 1.234568e+08
Conversion f: 123456.789000
Conversion g: 123.456
Conversion a: 0x1.edd2f1a9fbe77p6
Conversion t: 18:02:27
Conversion %: Nothing
Conversion n: Just a line
格式说明符语法说明
general、character、numeric 类型的转换符
语法如下:
%[argument_index$][flags][width][.precision]conversion
可选的 argument_index 是一个十进制整数,指明参数列表中参数的位置。从 1$ 开始。
可选的 flags 是一系列字符,用于修改输出格式。可用值和转换符相关。
可选的 width 是一个正十进制数,用于指明输出字符的数量。(最小宽度)
可选的 precision 是一个非负整数,通常用于限制字符的数量。行为和转换符相关。
date/time 类型的转换符
语法如下:
%[argument_index$][flags][width]conversion
参考资料
- 《Java 核心技术 卷一 基础知识》3.7.2 节