The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Earlier you saw the use of the 早些时候,您看到了使用print
and println
methods for printing strings to standard output (System.out
).print
和println
方法将字符串打印到标准输出(System.out
)。Since all numbers can be converted to strings (as you will see later in this lesson), you can use these methods to print out an arbitrary mixture of strings and numbers.由于所有数字都可以转换为字符串(您将在本课后面看到),因此可以使用这些方法打印字符串和数字的任意组合。The Java programming language has other methods, however, that allow you to exercise much more control over your print output when numbers are included.但是,Java编程语言还有其他方法,允许您在包含数字时对打印输出进行更多的控制。
printf
和format
方法The java.io
package includes a PrintStream
class that has two formatting methods that you can use to replace print
and println
.java.io
包包括一个PrintStream
类,该类有两种格式化方法,可用于替换print
和println
。These methods, format
and printf
, are equivalent to one another.format
和printf
这两种方法彼此等效。The familiar 您所使用的熟悉的System.out
that you have been using happens to be a PrintStream
object, so you can invoke PrintStream
methods on System.out
.System.out
恰好是一个PrintStream
对象,因此您可以在System.out
上调用PrintStream
方法。Thus, you can use 因此,您可以在代码中以前使用过format
or printf
anywhere in your code where you have previously been using print
or println
.print
或println
的任何地方使用format
或printf
。For example,例如
System.out.format(.....);
The syntax for these two java.io.PrintStream methods is the same:这两个java.io.PrintStream方法的语法相同:
public PrintStream format(String format, Object... args)
where 其中format
is a string that specifies the formatting to be used and args
is a list of the variables to be printed using that formatting.format
是一个字符串,指定要使用的格式,args
是要使用该格式打印的变量列表。A simple example would be一个简单的例子是
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);
The first parameter, 第一个参数format
, is a format string specifying how the objects in the second parameter, args
, are to be formatted.format
是一个格式字符串,指定如何格式化第二个参数args
中的对象。The format string contains plain text as well as format specifiers, which are special characters that format the arguments of 格式字符串包含纯文本和格式说明符,它们是格式化Object... args
.Object... args
参数的特殊字符。(The notation (符号Object... args
is called varargs, which means that the number of arguments may vary.)Object... args
称为varargs,这意味着参数的数量可能会有所不同。)
Format specifiers begin with a percent sign (%) and end with a converter.格式说明符以百分号(%
)开头,以转换器结尾。The converter is a character indicating the type of argument to be formatted.转换器是一个字符,指示要格式化的参数类型。In between the percent sign (%) and the converter you can have optional flags and specifiers.在百分号(%
)和转换器之间,可以有可选的标志和说明符。There are many converters, flags, and specifiers, which are documented in java.util.Formatterjava.util.Formatter中记录了许多转换器、标志和说明符
Here is a basic example:以下是一个基本示例:
int i = 461012; System.out.format("The value of i is: %d%n", i);
The %d
specifies that the single variable is a decimal integer.%d
指定单个变量为十进制整数。The %n
is a platform-independent newline character.%n
是独立于平台的换行符。The output is:输出为:
The value of i is: 461012
The printf
and format
methods are overloaded.printf
和format
方法重载。Each has a version with the following syntax:每个版本都有一个具有以下语法的版本:
public PrintStream format(Locale l, String format, Object... args)
To print numbers in the French system (where a comma is used in place of the decimal place in the English representation of floating point numbers), for example, you would use:例如,要在法语系统中打印数字(在英文浮点数表示法中,逗号用于替换小数点),您可以使用:
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);
The following table lists some of the converters and flags that are used in the sample program, 下表列出了下表后面的示例程序TestFormat.java
, that follows the table.TestFormat.java
中使用的一些转换器和标志。
d | ||
f | ||
n | %n , rather than \n .%n ,而不是\n 。 | |
tB | ||
td, te | ||
ty, tY | ||
tl | ||
tM | ||
tp | ||
tm | ||
tD | ||
08 | ||
+ | ||
, | ||
- | ||
.3 | ||
10.3 |
The following program shows some of the formatting that you can do with 下面的程序显示了可以使用format
.format
进行的一些格式化。The output is shown within double quotes in the embedded comment:输出显示在嵌入注释的双引号内:
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
and printf
methods.format
方法和printf
方法的基础知识。String.format
to create strings is covered in Strings.String.format
创建字符串。DecimalFormat
类You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.您可以使用java.text.DecimalFormat类来控制前导零和尾随零、前缀和后缀、分组(千)分隔符和十进制分隔符的显示。DecimalFormat
offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.DecimalFormat
在数字格式方面提供了很大的灵活性,但它会使代码更加复杂。
The example that follows creates a 下面的示例通过将模式字符串传递给DecimalFormat
object, myFormatter
, by passing a pattern string to the DecimalFormat
constructor.DecimalFormat
构造函数来创建DecimalFormat
对象myFormatter
。The format()
method, which DecimalFormat
inherits from NumberFormat
, is then invoked by myFormatter
DecimalFormat
从NumberFormat
继承的format()
方法随后由myFormatter
调用it accepts a 它接受double
value as an argument and returns the formatted number in a string:double
值作为参数,并以字符串形式返回格式化的数字:
Here is a sample program that illustrates the use of 下面是一个示例程序,演示了DecimalFormat
: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); } }
The output is:输出为:
123456.789 , . 123,456.789 123456.789 .## 123456.79 123.78 000000.000 000123.780 12345.67 $ , . $12,345.67
The following table explains each line of output.下表解释了每行输出。
123456.789 | , . | 123,456.789 | |
123456.789 | .## | 123456.79 | value has three digits to the right of the decimal point, but the pattern has only two.value 在小数点右侧有三位数字,但pattern 只有两位。format method handles this by rounding up.format 方法通过向上取整来处理此问题。 |
123.78 | 000000.000 | 000123.780 | pattern specifies leading and trailing zeros, because the 0 character is used instead of the pound sign (#).pattern 指定前导零和尾随零,因为使用0字符而不是磅符号(# )。 |
12345.67 | $ , . | $12,345.67 | pattern is the dollar sign ($).pattern 中的第一个字符是美元符号($ )。output .output 中最左边的数字之前。 |