printf函数和java.util.Formatter

本文深入探讨了Java中String.format()函数的使用方法及特性,包括日期格式化、数值格式化等多种格式化功能,展示了其强大的应用潜力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值