6.1.2、格式化数字输出

这篇Java教程基于JDK1.8。教程中的示例和实践不会使用未来发行版中的优化建议。
格式化数字输出

之前我们使用 printprintln 方法将字符串输出到标准输出。由于所有的数值都可以转换成 String 类型,你将看到这些方法可以打印数值和字符串的任意组合。在Java语言中,提供了额外的方法,能够让你在包含数值的字符串输出中有更多的控制权。

printf和格式化方法

java.io 包提供了一个 PrintStream 类有两种格式化方法用来代替printprintlnformatprintf 这两个方法基本等价。我们熟悉的System.out ,其实是PrintStream 对象,可以在 System.out 对象上调用 PrintStream 的方法。这样,在之前使用printprintln 的地方都可以用printfformat 代替。比如:

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之间,有可选的flagsconverters*。有许多的convertsflagsspecifiers,具体请参见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

printfformat 方法都可以重载。每个方法都有一个如下版本的重载方法:

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使用的转换器和标记:

ConverterFlag解释
d十进制数值
ffloat数值
n换行符
tBA date & time conversion,本地化月份的全名
td,teA date & time conversion,2位数字表示的月份。td会用0填充,te不会天中
ty,tYA date & time conversion,ty—2位的年份,tY 4位的年份
tlA date & time conversion,12小时制的hour
tMA date & time conversion,2位数的minutes,必要时前面用0填充
tpA date & time conversion,本地化的 am/pm
tmA date & time conversion,2位数的months,必要时前面填充0
tDA date & time conversion,日期就像 %tm%td%ty
088字符宽度,必要时进行填充
+包含正负号
本地化分组符号
-左适应
.3保留小数点后3位
10.310个字符宽度,右适应,小数点后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()方法,DecimalFormatNumberFormat继承来的方法,接着被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.78000000.000000123.780Pattern 指定了前导和后置0,因为用0字符代替了*#*
12345.67$###,###.###$12,345.67pattern 的第一个字符是*$*。注意,格式化输出中最左边的数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值