java printf格式笔记

本文详细介绍了Java中使用System.out.printf进行格式化输出的方法,包括占位符使用、浮点数保留位数、千分位分隔显示、对齐方式、百分比输出、补零填充及日期时间格式化等。

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

import org.junit.Test;

public class TestMe {

	@Test
	public void test() throws IOException {
		// 占位符指定使用哪个参数填充值
		System.out.printf("%2$s %2$s %1$s \n", "111", "222");
		// 111 111 222

		// 浮点数保留小数点后尾数(四舍五入取整)
		System.out.printf("%.2f \n", 1.256F);
		// 1.23

		// 千分位分割输出整型
		System.out.printf("%,d \n", 12345678);
		// 12,345,678

		// 默认右对齐
		System.out.printf("%12d| %15d|  \n", 12345678, 666666666);
		// 左对齐,不足补空格
		System.out.printf("%-12d| %-15d|   \n", 12345678, 666666666);
		System.out.println();

		// '%%'输出百分号%%
		System.out.printf("%d%% \n", 58);

		// 不足位补零
		System.out.printf("%05d \n", 98);
		// 00058

		// 负数将添加括号
		System.out.printf("%(d \n", -98);
		// (98)

		System.out.printf("%tF  \n", System.currentTimeMillis());
		// 14:28:07

		System.out.printf("%tT  \n", System.currentTimeMillis());
		// 2013-08-15
	}
}

 

参考:http://java.chinaitlab.com/base/835591.html 

 

 

--end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值