Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > left-padding ints

Reply
Thread Tools

left-padding ints

 
 
Ike
Guest
Posts: n/a
 
      09-07-2005
Is there something in the API for left-padding an int with, say, 0's when
converting to a String, such that, say 1 becomes "01" ? -Ike


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      09-07-2005
Ike <> wrote:
> Is there something in the API for left-padding an int with, say, 0's when
> converting to a String, such that, say 1 becomes "01" ? -Ike


Yep. See java.text.DecimalFormat or java.text.NumberFormat. For
DecimalFormat, simply use '0' characters instead of '#' characters in
the pattern. For NumberFormat, use setMinimumIntegerDigits. The
difference between the two is that NumberFormat is more flexible. If
you're sure that you want to display the result in a decimal number
system regardless of locale, then use DecimalFormat instead.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-07-2005
On Wed, 07 Sep 2005 13:48:02 GMT, "Ike" <> wrote or
quoted :

>Is there something in the API for left-padding an int with, say, 0's when
>converting to a String, such that, say 1 becomes "01" ? -Ike


make a slight mod to this code:

/**
* Pads the string out to the given length by applying blanks on
the left.
*
* @param s
* String to be padded/chopped.
* @param newLen
* length of new String desired.
* @param chop
* true if Strings longer than newLen should be truncated
to newLen
* chars.
* @return String padded on left/chopped to the desired length.
*/
public final static String leftPad ( String s, int newLen, boolean
chop )
{
int grow = newLen - s.length();
if ( grow <= 0 )
{
if ( chop )
{
return s.substring( 0, newLen );
}
else
{
return s;
}
}
else if ( grow <= 30 )
{
return " ".substring( 0, grow
) + s;
}
else
{
return rep( ' ', grow ) + s;
}
} // end leftPad

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      09-08-2005

"Ike" <> wrote in message
news:mcCTe.2144$ ink.net...
> Is there something in the API for left-padding an int with,
> say, 0's when
> converting to a String, such that, say 1 becomes "01" ? -Ike


// requires java 5.0
// not tested
int x = 5;
String s = String.format("%02d", x); // zero not oh

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hashCode for 4 ints? Jeff Java 5 06-22-2005 03:00 AM
Iterator Question for map of ints to set of ints uclamathguy@gmail.com C++ 3 04-03-2005 03:26 AM
ints ints ints and ints Skybuck Flying C Programming 24 07-10-2004 04:48 AM
Java2D: not possible to set 32bpp ints? Timo Nentwig Java 6 05-08-2004 12:22 AM
Why constant ints in switch case expressions? Brian J. Sayatovic Java 22 07-09-2003 09:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57