Java中數字前補零的方法,數字加減前補零函式
阿新 • • 發佈:2018-11-27
-
* 將元資料前補零,補後的總長度為指定的長度,以字串的形式返回
-
* @param sourceDate
-
* @param formatLength
-
* @return 重組後的資料
-
*/
-
public static String frontCompWithZore(int sourceDate,int formatLength)
-
{
-
/*
-
* 0 指前面補充零
-
* formatLength 字元總長度為 formatLength
-
* d 代表為正數。
-
*/
-
String newString = String.format("%0"+formatLength+"d", sourceDate);
-
return newString;
-
}
如:
-
String newString = String.format("%02d", 5);
-
System.out.println("newString === "+newString);
執行結果:
newString === 05