Java - Write code to format int with new DecimalFormat("000")

Requirements

Write code to format int with new DecimalFormat("000")

Demo

//package com.book2s;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) {
        int val = 42;
        System.out.println(int2char3(val));
    }// w  w  w  .j a  v  a2  s.c  o m

    public static String int2char3(int val) {
        DecimalFormat fmt = new DecimalFormat("000");

        if ((val < 0) || (val > 999))
            return null;
        return fmt.format(val);
    }
}

Related Exercise