The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Zero-Padding

Posted by Matt Gerrans on November 15, 2001 at 5:49 PM

Try something like this:


public class Xero
{
public static void main(String[] args)
{
for( int i = 0; i < 20; i++ )
System.out.println( ZeroPad(i, 2) );
}

static String ZeroPad( int number, int width )
{
StringBuffer result = new StringBuffer("");
for( int i = 0; i < width-Integer.toString(number).length(); i++ )
result.append( "0" );
result.append( Integer.toString(number) );
return result.toString();
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us