笔记06 格式化输出

格式化输出
/*
		补充课外知识(java5及以上版本新增的特性)
		
		1、格式化输出。
			1) 格式化输出目的是让输出的数据按照某种格式出现。
			
			2) 使用的语句为:  System.out.printf("信息<格式控制符>" , 数据);
			
			3) 常用的格式控制符:
				%d  用来控制十进制整数的格式.
				
				%f  用来控制小数的格式
				
				%c  用来控制字符的格式
				
				%s  用来控制字符串的格式.
				
				n   用来控制数据占n位
				n.m 用来控制数据占n位,小数占m位
				 .m 用来控制数据的小数占m位
				 
				n$  用来表示控制符对应的数据是第n个。
				<	从前
				
				,   整数每三位添加一个千位分隔符。
				0   空位补零.
				-   左对齐。(它与0不能同时使用)
*/
import java.util.Date ;

public  class  PrintfTest {
	public static void  main( String[] args ) {
		int a = 18;
		System.out.println("a=" + a  );
		System.out.printf("a=%d \n" , a );
		System.out.printf("a=%5d \n" , a );
		
		double d1 = 10.0 / 3 ;
		System.out.println("d1=" + d1 );
		System.out.printf("d1=%6.2f \n" , d1 );
		System.out.printf("d1=%.0f \n" , d1 );
		
		String name = "张三";
		
		System.out.printf("name=%1$s  name=%<s  a=%2$d   d1=%3$.2f \n" , name, a, d1 );
		
		d1 = 100000000 / 3.0;
		System.out.printf("d1=%,8.2f" , d1 );
		
		System.out.printf("\na=%06d" , a );
		System.out.printf("\na=%-6d" , a );
		
		System.out.printf("d1=%e\n" , d1 );
		
		System.out.printf("北京时间>>> %tF %<tT  %<tA \n" , new Date() );
	}
}


 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值