java logo 显示数字_Java™ 教程(格式化数字打印输出)

格式化数字打印输出

之前你已经看到使用print和println方法将字符串打印到标准输出(System.out),由于所有数字都可以转换为字符串(你将在本课后面看到),你可以使用这些方法打印出任意的字符串和数字混合,但是,Java编程语言还有其他方法,可以在包含数字时对打印输出进行更多控制。

printf和format方法

java.io包中包含一个PrintStream类,它有两种格式化方法可用于替换print和println,这些方法,format和printf,彼此相同。你一直使用的熟悉的System.out恰好是PrintStream对象,因此你可以在System.out上调用PrintStream方法,因此,你可以在以前使用print或println的代码中的任何位置使用format或printf,例如:

System.out.format(.....);

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);

第一个参数format是一个格式字符串,指定如何格式化第二个参数args中的对象,格式字符串包含纯文本和格式说明符,它们是格式化Object... args参数的特殊字符(符号Object... args称为可变参数,这意味着参数的数量可能会有所不同)。

格式说明符以百分号(%)开头,以转换器结束,转换器是一个字符,指示要格式化的参数类型,在百分号(%)和转换器之间,你可以使用可选的标志和说明符,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中使用的一些转换器和标志。

转换器

标志

说明

d

十进制整数

f

浮点数

n

适合于运行应用程序的平台的新行字符,你应该始终使用%n,而不是\n

tB

日期和时间转换 — 特定于语言环境的月份全名

td, te

日期和时间转换 — 2位数的月日,td根据需要有前导零,te没有

ty, tY

日期和时间转换 — ty = 2位数年份,tY = 4位数年份

tl

日期和时间转换 — 12小时制

tM

日期和时间转换 — 2位数分钟,必要时带前导零

tp

日期和时间转换 — 特定于语言环境的am/pm(小写)

tm

日期和时间转换 — 2位数的月份,必要时带有前导零

tD

日期和时间转换 — 日期为%tm%td%ty

08

宽度为八个字符,必要时带前导零

+

包括正负号

,

包含特定于语言环境的分组字符

-

左对齐..

.3

小数点后三位

10.3

宽度为十个字符,右对齐,小数点后三位

以下程序显示了你可以使用格式进行的一些格式化,输出显示在嵌入注释中的双引号内:

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"

}

}

本节中的讨论仅涵盖format和printf方法的基础知识

DecimalFormat类

你可以使用java.text.DecimalFormat类来控制前导和尾随零、前缀和后缀、分组(千)分隔符和小数分隔符的显示,DecimalFormat在数字格式化方面提供了极大的灵活性,但它使你的代码更复杂。

下面的示例通过将模式字符串传递给DecimalFormat构造函数来创建DecimalFormat对象myFormatter。然后,myFormatter会调用DecimalFormat从NumberFormat继承的format()方法 — 它接受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

下表说明了每行输出。

模式

输出

说明

123456.789

###,###.###

123,456.789

井号(#)表示一个数字,逗号是分组分隔符的占位符,点是小数分隔符的占位符。

123456.789

###.##

123456.79

该值在小数点右侧有三位数,但该模式只有两位,format方法通过舍入来处理这个问题。

123.78

000000.000

000123.780

该模式指定前导零和尾随零,因为使用0字符而不是井号(#)。

12345.67

$###,###.###

$12,345.67

模式中的第一个字符是美元符号($),请注意,它紧接在格式化输出中最左边的数字之前。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值