Java的一些格式化操作
大部分的內容可以食用:
https://www.cnblogs.com/Dhouse/p/7776780.html
格式化文本
System.out.println(String.format("長度不滿10個字符[左邊]補空格:[%10s]", "hello"));
System.out.println(String.format("長度不滿10個字符[右邊]補空格:[%-10s]", "hello"));
輸出:
長度不滿10個字符[左邊]補空格:[ hello]
長度不滿10個字符[右邊]補空格:[hello ]
格式化數字
int型
System.out.println(String.format("[%d]", 76));
System.out.println(String.format("[%4d]", 76));
System.out.println(String.format("[%-4d]", 76));
System.out.println(String.format("[%04d]", 76));
輸出
[76]
[ 76]
[76 ]
[0076]
float型
float f = 123.4561f;
System.out.println(String.format("[%f]", f));
System.out.println(String.format("[%.2f]", f));
System.out.println(String.format("[%.3f]", f));
輸出
[123.456100]
[123.46]
[123.456]
格式化日期時間
待完善