Left pad with zeros
Posted on 2006-11-17
I want a function that converts an int to String, with left padding of a specified number of zeros. I know I could do this with a loop, and count characters, but I want a simpler method. I thought there was something like that already in the JDK that did this.
Something like String leftPad(int numToCovert, int width) which would return the following:
leftPad(5, 3) would return "005"
leftPad(15, 5) would return "00015"
leftPad(224, 3) would return "224"
leftPad(0, 4) would return "0000"
leftPad(3335, 3) would throw an exception
I don't want you to write the method for me. I want you to tell me the JDK classes and methods that I should use. I would especially like to know if a "leftPad" method already exists.