Strings In Depth10 / 18
Up next

Zero-pad numbers

class Main {
    public static void main(String[] args) {
        // 05d pads with zeros to width 5
        System.out.println(String.format("%05d", 42));
        // +04d also forces the sign
        System.out.println(String.format("%+04d", -7));
    }
}
What you're solving

leading zeros with a format spec