|||
import java.io.*;
import java.util.*;
public class Eg{
public static void main(String[] args) {
//方法1: 使用 printf
System.out.printf("==Using printfn");
System.out.printf("%5.3fn",Math.PI);
// 方法2: Formatter类的使用
System.out.printf("==Using Formattern");
Formatter f = new Formatter(System.out);
f.format("%5.3fn", Math.PI);
// 方法3: 格式化输出到文件
System.out.printf("==Using Formatter in files see filename.txtn");
PrintStream fr=null;
try{
fr=new PrintStream("./filename.txt");
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
Formatter myfmat = new Formatter(fr);
myfmat.format("%5.2fn", Math.PI);
// 方法4: String.format().
System.out.printf("==Using String.formatn");
System.out.println(String.format("%5.2f", Math.PI));
}
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-13 14:58
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社