java string format

java string fromat 基本用法示例:

import java.util.Date;

/***
 * 
 * @author Administrator
 *    %[argument number] [flags] [width]  [.precision] type
 *eg: %         1$          ,       6            .1      f
 */  
public class TestFormat {
	public static void main(String[] args){
		String s = String.format("%, d", 1000000000);
		s = String.format("I have %.2f bugs to fix.", 429342.3483);
		s = String.format("%1$,6.1f", 42.000);
		s = String.format("%d" , 42);  // 格式化10进制
		s = String.format("%.3f", 42.0000); //格式化浮点型
		s = String.format("%x", 42);   // 格式化16进制
		s = String.format("%c", 42);   // 字符型格式化
		int one = 2014;
		double two = 2014.1018;
		s = String.format("The bank is %,d out of %,.2f", one,two); //按顺序替换
		//格式化时间
		s = String.format("%tc", new Date());  // date and time 
		s = String.format("%tr", new Date());  // just the time
		Date today = new Date();
		s = String.format("%tA, %tB %td", today,today,today);  // week month and day.
		s = String.format("%tA, %<tB %<td", today);  //和上面一样的效果,去除重复的today
		
		System.out.println(s);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值