`
tedeyang
  • 浏览: 319077 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

printf函数和java.util.Formatter

阅读更多

printf函数和java.util.Formatter

import java.util.Date;

import junit.framework.TestCase;

public class FormatterTest extends TestCase {
 public void testString() {
  System.out.println(String.format("%1$6s is a %2$10s", "this", "word"));
  System.out.println(String.format("%1$tF %1$tT = %2$tY-%2$tm-%2$td %2$tH:%2$tM:%2$tS ", new Date(), new Date()));
  System.out.println(String.format("%012.2f", 22122.33453));
  System.out.println(String.format("%,12.2f", 22122.33453));
  System.out.println(String.format("%10.2g", 22122.33453));
  System.out.println(String.format("%.6g", 22122.33453));
 }
}

输出

  this is a       word
2008-08-21 07:39:25 = 2008-08-21 07:39:25
000022122.33
   22,122.33
   2.2e+04
22122.3


JDK5新增的这个格式化功能非常强大,值得花时间看看 .

详细请参考 JDK手册中 java.util 类 Formatter一节.

 

【2008-8】

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics