1.前面自动补位,方式一:

    public static void main(String[] args) {
        int i = 12;
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setGroupingUsed(false);//设置是否使用分组
        nf.setMaximumIntegerDigits(4);//设置最大整数位数
        nf.setMinimumIntegerDigits(4);//设置最小整数位数    
        String num = nf.format(i);
        System.out.println("补位后:"+num);
    }

  输出结果:补位后:0012

2.前面自动补位,方式二:

    public static void main(String[] args) {
        int i = 89;      
        //0:代表前面补充0;4:代表长度为4;d:代表参数为正数型      
        String str = String.format("%04d", i);      
        System.out.println(str);
    }

输出结果:0089

3.全是数字的流水号,自增1之后补位:

    public static void main(String[] args) {
        String liuShuiHao = "0020190815";
        Integer intHao = Integer.parseInt(liuShuiHao);
        intHao++;
        String strHao = intHao.toString();
        while (strHao.length() < liuShuiHao.length())strHao = "0" + strHao;
        System.out.println("流水号:"+strHao);
    }

输出结果:流水号:0020190816

 

相关文章:

  • 2022-12-23
  • 2022-02-22
  • 2022-02-10
  • 2021-12-11
  • 2022-12-23
  • 2021-09-14
  • 2021-04-10
  • 2021-05-18
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2021-12-03
  • 2021-09-10
相关资源
相似解决方案