int转String 使用0来填充长度

来源:互联网 发布:软件出现乱码 编辑:程序博客网 时间:2024/06/10 18:21

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);