以前你已經看到使用print
和println
方法將字符串打印到標準輸出(System.out
),因爲全部數字均可以轉換爲字符串(你將在本課後面看到),你可使用這些方法打印出任意的字符串和數字混合,可是,Java編程語言還有其餘方法,能夠在包含數字時對打印輸出進行更多控制。html
java.io
包中包含一個PrintStream
類,它有兩種格式化方法可用於替換print
和println
,這些方法,format
和printf
,彼此相同。你一直使用的熟悉的System.out
剛好是PrintStream
對象,所以你能夠在System.out
上調用PrintStream
方法,所以,你能夠在之前使用print
或println
的代碼中的任何位置使用format
或printf
,例如:java
System.out.format(.....);
這兩個java.io.PrintStream方法的語法是相同的:編程
public PrintStream format(String format, Object... args)
其中format
是一個字符串,用於指定要使用的格式,args
是要使用該格式打印的變量列表,一個簡單的例子就是:segmentfault
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
稱爲可變參數,這意味着參數的數量可能會有所不一樣)。api
格式說明符以百分號(%
)開頭,以轉換器結束,轉換器是一個字符,指示要格式化的參數類型,在百分號(%
)和轉換器之間,你可使用可選的標誌和說明符,java.util.Formatter中記錄了許多轉換器、標誌和說明符。oracle
這是一個基本的例子:編程語言
int i = 461012; System.out.format("The value of i is: %d%n", i);
%d
指定單個變量是十進制整數,%n
是與平臺無關的換行符,輸出是:函數
The value of i is: 461012
printf和format方法有重載方法,每一個都有一個版本,其語法以下:code
public PrintStream format(Locale l, String format, Object... args)
例如,要在法語系統中打印數字(使用逗號代替浮點數的英文表示中的小數位),你將使用:orm
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
方法的基礎知識
你可使用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 | 模式中的第一個字符是美圓符號($ ),請注意,它緊接在格式化輸出中最左邊的數字以前。 |