Java Data Type How to - Add leading zeroes to a number








Question

We would like to know how to add leading zeroes to a number.

Answer

public class Main {
  public static void main(String[] args) {
    int number = 1500;

    String formatted = String.format("%07d", number);

    System.out.println("Number with leading zeros: " + formatted);
  }
}

The code above generates the following result.