Solved

Left pad with zeros

Posted on 2006-11-17
4
20,088 Views
Last Modified: 2011-08-18
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.
0
Comment
Question by:mhunts
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17967747
you should use NumberFormat class in order to do this
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17967748
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17967867
public static String leftPad(int n, int padding) {
    return String.format("%0" + padding + "d", n);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17968390
:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 84
count8 challlenge 13 87
best (free) software to access postgres db (java) 1 18
unix in java example 9 43
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now