Java - Write code to format Zero Leading Long

Requirements

Write code to format Zero Leading Long

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        long n = 42;
        int digits = 42;
        System.out.println(formatZeroLeadingLong(n, digits));
    }//from  www  . j  a va  2 s . c  o m

    public static String formatZeroLeadingLong(long n, int digits) {
        /*
         * we create a format : %% : % the first % is to escape the second % 0 :
         * 0 zero character %d : how many '0' we want (specified by digits) d :
         * d the number to format
         */
        String format = String.format("%%0%dd", digits);
        return String.format(format, n);
    }
}