java printf格式



java printf格式

Java代码 收藏代码
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