Java中格式化输出的几种方法

来源:互联网 发布:查重软件是什么 编辑:程序博客网 时间:2024/06/04 01:10
Java中实现格式化输出的几种方式:
1、System.out.printf();
类似于c语言的printf方法。如:
int x = 55;
System.out.format(“x = %5x”, x);
输出结果为:x = 37
2、System.out.format()
int x = 55;
System.out.printf(“x = %5c”, x);
输出结果为:x = 7
3、String.format()
String提供的静态方法。
int x = 55;
System.out.println(String.format(“x = %5x”, x));
输出结果为:x = 37
4、Formatter
int x = 55;
Formatter format = new Formatter(System.out);
format.format(“x = %5x”, x);
输出结果为:x = 37
0 0