这篇Java教程基于JDK1.8。教程中的示例和实践不会使用未来发行版中的优化建议。
格式化数字输出
之前我们使用 print 和 println 方法将字符串输出到标准输出。由于所有的数值都可以转换成 String 类型,你将看到这些方法可以打印数值和字符串的任意组合。在Java语言中,提供了额外的方法,能够让你在包含数值的字符串输出中有更多的控制权。
printf和格式化方法
java.io 包提供了一个 PrintStream 类有两种格式化方法用来代替print 和 println 。format 和 printf 这两个方法基本等价。我们熟悉的System.out ,其实是PrintStream 对象,可以在 System.out 对象上调用 PrintStream 的方法。这样,在之前使用print 和println 的地方都可以用printf 和 format 代替。比如:
System.out.format(.....);
这两个* java.io.PrintStream* 方法的语法类型:
public PrintStream format(String format, Object... args)
参数format是格式化的一种描述,args 则是将打印的变量列表。示例如下:
System.out.format("The value of " + "the float variable is " +
"%f, while the value of the " + "integer variable is %d, " +
"and the string is %s", floatVar, intVar, stringVar);
格式化占位符以*%开始以converter结尾。转换器是个字符指明了被格式化参数的类型。在%和converter之间,有可选的flags和converters*。有许多的converts,flags和specifiers,具体请参见java.util.Formatter。
请看下面的示例:
int i = 461012;
System.out.format("The value of i is: %d%n", i);
%d 表示单变量是十进制数值。%n是平台独立的换行符。输出如下:
The value of i is: 461012
printf和format 方法都可以重载。每个方法都有一个如下版本的重载方法:
public PrintStream format(Locale l, String format, Object... args)
为了在法语系统中打印数值,你可以:
System.out.format(Locale.FRANCE,
"The value of the float " + "variable is %f, while the " +
"value of the integer variable " + "is %d, and the string is %s%n",
floatVar, intVar, stringVar);
示例
下表列出了示例程序TestFormat.java使用的转换器和标记:
Converter | Flag | 解释 |
---|---|---|
d | 十进制数值 | |
f | float数值 | |
n | 换行符 | |
tB | A date & time conversion,本地化月份的全名 | |
td,te | A date & time conversion,2位数字表示的月份。td会用0填充,te不会天中 | |
ty,tY | A date & time conversion,ty—2位的年份,tY 4位的年份 | |
tl | A date & time conversion,12小时制的hour | |
tM | A date & time conversion,2位数的minutes,必要时前面用0填充 | |
tp | A date & time conversion,本地化的 am/pm | |
tm | A date & time conversion,2位数的months,必要时前面填充0 | |
tD | A date & time conversion,日期就像 %tm%td%ty | |
08 | 8字符宽度,必要时进行填充 | |
+ | 包含正负号 | |
, | 本地化分组符号 | |
- | 左适应 | |
.3 | 保留小数点后3位 | |
10.3 | 10个字符宽度,右适应,小数点后3位 |
下面的示例展示了使用 format 的格式化。输出用双引号括起来的注释予以展示:
import java.util.Calendar;
import java.util.Locale;
public class TestFormat {
public static void main(String[] args) {
long n = 461012;
System.out.format("%d%n", n); // --> "461012"
System.out.format("%08d%n", n); // --> "00461012"
System.out.format("%+8d%n", n); // --> " +461012"
System.out.format("%,8d%n", n); // --> " 461,012"
System.out.format("%+,8d%n%n", n); // --> "+461,012"
double pi = Math.PI;
System.out.format("%f%n", pi); // --> "3.141593"
System.out.format("%.3f%n", pi); // --> "3.142"
System.out.format("%10.3f%n", pi); // --> " 3.142"
System.out.format("%-10.3f%n", pi); // --> "3.142"
System.out.format(Locale.FRANCE,
"%-10.4f%n%n", pi); // --> "3,1416"
Calendar c = Calendar.getInstance();
System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"
System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"
System.out.format("%tD%n", c); // --> "05/29/06"
}
}
DecimalFormat 类
使用 * java.text.DecimalFormat * 能够控制前导或后置0,前缀和后缀,分隔符(千分组)以及十进制分隔符。DecimalFormat 提供了在数字格式化方面提供了很好的方案,但它会让代码更复杂。
示例先创建一个DecimalFormat 对象 myFormatter ,通过传递一个模式字符串给 DecimalFormat构造器。format()方法,DecimalFormat从NumberFormat继承来的方法,接着被myFormatter调用—它接受double类型的参数并返回格式化后的数值字符串。
下面的程序演示了如何使用DecimalFormat:
import java.text.*;
public class DecimalFormatDemo {
static public void customFormat(String pattern, double value ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void main(String[] args) {
customFormat("###,###.###", 123456.789);
customFormat("###.##", 123456.789);
customFormat("000000.000", 123.78);
customFormat("$###,###.###", 12345.67);
}
}
输出为:
123456.789 ###,###.### 123,456.789
123456.789 ###.## 123456.79
123.78 000000.000 000123.780
12345.67 $###,###.### $12,345.67
下表对每一行输出进行了解释:
值 | Pattern | 输出 | 解释 |
---|---|---|---|
123456.789 | ###,###.### | 123,456.789 | *#*代表一个数字,逗号是分组分隔符的占位符,句号是十进制分隔符的占位符。 |
123456.789 | ###.## | 123456.79 | 十进制分隔符右边有3位数字,但Pattern只有2位。格式化方法通过进位来处理这种情况 |
123.78 | 000000.000 | 000123.780 | Pattern 指定了前导和后置0,因为用0字符代替了*#* |
12345.67 | $###,###.### | $12,345.67 | pattern 的第一个字符是*$*。注意,格式化输出中最左边的数字。 |