-1

How can I write a method with this header

public static String format (int number, int width)

that would return a string for the number with one or more prefix 0s. the size of the string is the width? example if use use format(43, 4) the output is 0043.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245

1 Answers1

3

Try this:

public static String format(int number, int width) {
    return String.format("%0" + width + "d", number);
}
arshajii
  • 127,459
  • 24
  • 238
  • 287