int轉String 使用0來填充長度

import java.text.DecimalFormat;
//(1)、如果數字1是字符串,如下處理:
String str1=”1”;
DecimalFormat df=new DecimalFormat(“0000”);
String str2=df.format(Integer.parseInt(str1));
System.out.println(str2);
//(2)、如果數字1是整型,如下處理:
int str1=1;
DecimalFormat df=new DecimalFormat(“0000”);
String str2=df.format(str1);
System.out.println(str2);

簡便的寫法
String str=String.format(“%05d”,22);

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