Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > force string made from integer to be n-digit

Reply
Thread Tools

force string made from integer to be n-digit

 
 
Thomas Jollans
Guest
Posts: n/a
 
      02-02-2004
Is there a function (or way) to make 1 (an int) to "01", 16 to "16", 3
to "03"...? I will also need to do this with 4-digit numbers.

thomas
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      02-02-2004
Thomas Jollans wrote:

> Is there a function (or way) to make 1 (an int) to "01", 16 to "16", 3
> to "03"...? I will also need to do this with 4-digit numbers.
>
> thomas


Something like:

int REQUIRED_LENGTH = 4;

String s = String.valueOf("5");
int sizeDiff = REQUIRED_LENGTH - s.size();

StringBuffer prefix = new StringBuffer();
for(int i = 0;i<sizeDiff;i++){
prefix.append("0");
}
prefix.append(s);

s = prefix.toString;


should work.

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Carl Howells
Guest
Posts: n/a
 
      02-02-2004
Thomas Jollans wrote:
> Is there a function (or way) to make 1 (an int) to "01", 16 to "16", 3
> to "03"...? I will also need to do this with 4-digit numbers.
>
> thomas


Take a look at java.text.DecimalFormat.
 
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
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM



Advertisments