Java int转string 长度不足左补0

最近项目中用到这个需求,我试了两个方法都可以实现

方法一:(推荐)


String s=String.format("%010d", 123)//123为int类型,0代表前面要补的字符 10代表字符串长度,d表示参数为整数类型

方法二:


String s1=new DecimalFormat("0000000000").format(123)//123为int类型

上面的s和s1输出结果都为:0000000123

原文地址:https://www.cnblogs.com/suizhikuo/p/9836718.html