在Java中用零左垫整数

首先让我们看一个例子,以了解整数如何用左填充-

888 //left padding with spaces
0000000999 //left padding with 7 zeros

让我们看一个例子,左数填充零-

示例

public class Demo {
   public static void main(String[] args) {
      int val = 9899;
      System.out.println(String.format("%05d",val));
   }
}

输出结果

09899

让我们看另一个填充更多零的示例-

示例

public class Demo {
   public static void main(String[] args) {
      int val = 9899;
      System.out.println(String.format("%010d", val));
   }
}

输出结果

0000009899