Java的一些格式化操作的記錄(非全)_2019-10-14

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]

格式化日期時間

待完善

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章