Java String字符串补0或空格

 1    /**
 2      * 给数字左边补0
 3      */
 4     public static String completion0(int i){
 5         // 得到一个NumberFormat的实例
 6         NumberFormat nf = NumberFormat.getInstance();
 7         // 设置是否使用分组
 8         nf.setGroupingUsed(false);
 9         // 设置最大整数位数
10         nf.setMaximumIntegerDigits(4);
11         // 设置最小整数位数
12         nf.setMinimumIntegerDigits(4);
13         // 输出测试语句
14         return nf.format(i);
15     }
 1    /**
 2      * 数字转字符串前面自动补0的实现
 3      */
 4     public static String intToStr(int yourNumber) {
 5         // 0 代表前面补充0
 6         // 4 代表长度为4
 7         // d 代表参数为正数型
 8         String str = String.format("%04d", yourNumber);
 9         return str; // 0001
10     }
1    /**
2      * 流水号加1后返回,流水号长度为4
3      */
4     public static String haoAddOne_2(String liuShuiHao){
5         Integer intHao = Integer.parseInt(liuShuiHao);
6         intHao++;
7         DecimalFormat df = new DecimalFormat("0000");
8         return df.format(intHao);
9     }

 By LiYing

原文地址:https://www.cnblogs.com/widgetbox/p/11693300.html