程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JDK5新特性之新的格式化輸出

JDK5新特性之新的格式化輸出

編輯:關於JAVA
JDK5.0允許象C語言那樣直接用printf()方法來格式化輸出,並且提供了許多參數來格式化輸入,調用也很簡單:

System.out.format("Pi is approximately %f", Math.Pi);

System.out.printf("Pi is approximately %f", Math.Pi);

printf()和 format() 方法具有相同的功能. System.out 是 java.io.PrintStream的實例. PrintStream, java.io.PrintWriter, 和 java.lang.String 每個類都有四個新的格式化方法:

format( String format, Object... args);

printf( String format, Object... args);

format( Locale locale, String format, Object... args);

printf( Locale locale, String format, Object... args);

同時,以前的formatter類也提供了更完善的方法來格式化,例如:

formatter.format("Pi is approximately %1$f," +

"and e is about %2$f", Math.PI, Math.E);


格式化元素的構成如下:

%[argument_index$][flags][width][.precision]conversion

其中:

argument_index是一個正整數,說明了參數的位置,1為取第一個參數

width表示輸出的最小字母個數

precision代表數字的小數位數

conversion代表被格式化的參數的類型:

f float,

t time

d decimal

o octal

x hexadecimal

s general

c a Unicode character

以下是個例子:

package format;

import java.util.Formatter;

public class UsingFormatter {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingFormatter ");

System.exit(0);

}

String format = args[0];

StringBuilder stringBuilder = new StringBuilder();

Formatter formatter = new Formatter(stringBuilder);

formatter.format("Pi is approximately " + format +

", and e is about " + format, Math.PI, Math.E);

System.out.println(stringBuilder);

}

}

//控制台調用

java format/UsingFormatter %f

//輸出

Pi is approximately 3.141593, and e is about 2.718282

//控制台調用

java format/UsingFormatter %.2f

//輸出

Pi is approximately 3.14, and e is about 2.72

//控制台調用

java format/UsingFormatter %6.2f

//輸出(有空格來填補長度)

Pi is approximately 3.14, and e is about 2.72

//控制台調用

java format/UsingFormatter %1$.2f

//輸出

Pi is approximately 3.14, and e is about 3.14

//改變區域設置

package format;

import java.util.Formatter;

import java.util.Locale;

public class UsingFormatter {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingFormatter <format string>");

System.exit(0);

}

String format = args[0];

StringBuilder stringBuilder = new StringBuilder();

Formatter formatter = new Formatter(stringBuilder,

Locale.FRANCE);

formatter.format("Pi is approximately " + format +

", and e is about " + format, Math.PI, Math.E);

System.out.println(stringBuilder);

}

}

//控制台調用

java format/UsingFormatter %.2f

//輸出

Pi is approximately 3,14, and e is about 2,72

//采用format,printf的可替代寫法

package format;

public class UsingSystemOut {

public static void main(String[] args) {

if (args.length != 1) {

System.err.println("usage: " +

"java format/UsingSystemOut <format string>");

System.exit(0);

}

String format = args[0];

System.out.format("Pi is approximately " + format +

", and e is approximately " + format, Math.PI, Math.E);

}

}

//控制台調用

java format/UsingSystemOut %.2f%n

//輸出

Pi is approximately 3.14

, and e is about 2.72

對時間的格式化用字母t來代表,通常在t後面加上特殊的字符來顯示時間的某個部分:

tr hour and minute,

tA the day of the week

tB the name of the month

te the number of the day of the month

tY the year

//eg.

package format;

import java.util.Calendar;

public class FormattingDates {

public static void main(String[] args) {

System.out.printf("Right now it is %tr on " +

"%<tA, %<tB %<te, %<tY.%n",

Calendar.getInstance());

}

}

//說明:“<”指示采用的參數為前一個被格式化的參數

//輸出

Right now it is 01:55:19 PM on Wednesday, September 22, 2004.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved