cliffgao的个人博客分享 http://blog.sciencenet.cn/u/cliffgao 兴趣:生物信息学、统计、概率

博文

Java 字符串 格式化输出

已有 5441 次阅读 2013-10-14 15:24 |个人分类:Java|系统分类:科研笔记| Java

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

   }  

}




https://wap.sciencenet.cn/blog-468005-732877.html

上一篇:2013-09-10 教师节快乐!
下一篇:推荐《与青年朋友谈科研与学习策略》
收藏 IP: 111.30.45.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 08:48

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部