Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Number format (ex: 5 as 00005)

Reply
Thread Tools

Number format (ex: 5 as 00005)

 
 
m
Guest
Posts: n/a
 
      11-09-2006
Hi All

Can you please tell me a way in which I can display a number with
leading 0's. The total length of the number will be 5 digits.

Ex: 5 as 00005, 25 as 00025, 125 as 00125 etc.
I have a sequence which goes from 1 to a max of 99999.

I know I can do it like this ..
If (length(number)==1) { "0000"+number}
else if (length(number)==2) { "000"+number}

I am looking for any better solution. I use jdk1.5.0

Thanks

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      11-09-2006
m wrote:
> Can you please tell me a way in which I can display a number with
> leading 0's. The total length of the number will be 5 digits.
>
> Ex: 5 as 00005, 25 as 00025, 125 as 00125 etc.
> I have a sequence which goes from 1 to a max of 99999.
>
> I know I can do it like this ..
> If (length(number)==1) { "0000"+number}
> else if (length(number)==2) { "000"+number}
>
> I am looking for any better solution. I use jdk1.5.0


In Java 1.5 you should look at printf.

Example:

int v = 123;
System.out.printf("%05d", v);

It can also be used in other contexts than
System.out !

Arne
 
Reply With Quote
 
 
 
 
djthomp
Guest
Posts: n/a
 
      11-09-2006
On Nov 9, 3:04 pm, "m" <(E-Mail Removed)> wrote:
> Hi All
>
> Can you please tell me a way in which I can display a number with
> leading 0's. The total length of the number will be 5 digits.
>
> Ex: 5 as 00005, 25 as 00025, 125 as 00125 etc.
> I have a sequence which goes from 1 to a max of 99999.
>
> I know I can do it like this ..
> If (length(number)==1) { "0000"+number}
> else if (length(number)==2) { "000"+number}
>
> I am looking for any better solution. I use jdk1.5.0
>
> Thanks


I'd suggest taking a look at String.format
http://java.sun.com/j2se/1.5.0/docs/...va.lang.Object...)

 
Reply With Quote
 
djthomp
Guest
Posts: n/a
 
      11-09-2006
Hmn, that link didn't work so well with the Object... arg at the end.

Probably just better to point you towards
http://java.sun.com/j2se/1.5.0/docs/...Formatter.html

 
Reply With Quote
 
Mark Jeffcoat
Guest
Posts: n/a
 
      11-09-2006
"m" <(E-Mail Removed)> writes:

> Hi All
>
> Can you please tell me a way in which I can display a number with
> leading 0's. The total length of the number will be 5 digits.
>
> Ex: 5 as 00005, 25 as 00025, 125 as 00125 etc.
> I have a sequence which goes from 1 to a max of 99999.
>
> I know I can do it like this ..
> If (length(number)==1) { "0000"+number}
> else if (length(number)==2) { "000"+number}
>
> I am looking for any better solution. I use jdk1.5.0
>


Well, if you interpret the number as a String
(e.g., Integer.toString()), and remember that the
String has a length(), you can calculate the number of
leading 0's you need make the total length() == 5.

--
Mark Jeffcoat
Austin, TX
 
Reply With Quote
 
m
Guest
Posts: n/a
 
      11-09-2006
Thanks for this.. Looks great.
int v = 123;
System.out.printf("%05d", v);

can you please tell me how I can use in other context , like if I want
to concat another string to it.
Ex: 00123abcd
How can I add another string which has "abcd" and assign 00123abcd to a
string variable.

 
Reply With Quote
 
m
Guest
Posts: n/a
 
      11-09-2006
solved like this

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class numberfor{

public static void main (String[] args){
int v = 123;
//System.out.printf("%05d", v);
NumberFormat formatter = new DecimalFormat("00000");
String s = formatter.format(v);
System.out.println(s);

}

}

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      11-09-2006
m wrote:
> can you please tell me how I can use in other context , like if I want
> to concat another string to it.
> Ex: 00123abcd
> How can I add another string which has "abcd" and assign 00123abcd to a
> string variable.


The following should show some of the possibilities:

import java.io.*;

public class Fmt {
public static void main(String[] args) throws IOException {
int v = 123;
System.out.printf("%05d\n", v);
System.out.printf("%05dxyz%s\n", v, ".");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.printf("%05dxyz%s", v,".");
pw.close();
String s = sw.toString();
System.out.println(s);
PrintWriter pwf = new PrintWriter(new FileWriter("C:\\z.txt"));
pwf.printf("%05dxyz%s", v,".");
pwf.close();
String sf = String.format("%05dxyz%s", v,".");
System.out.println(sf);
}
}

Arne
 
Reply With Quote
 
Paul Hamaker
Guest
Posts: n/a
 
      11-09-2006
DecimalFormat dfm = new DecimalFormat("00000");
String s1 = dfm.format(number);
String s2 = "abcd" ;
String s3 = s1 + s2 ;
--
http://javalessons.com Paul Hamaker, SEMM
Teaching ICT since 1987

 
Reply With Quote
 
Robert Mabee
Guest
Posts: n/a
 
      11-10-2006
Mark Jeffcoat wrote:
> "m" <(E-Mail Removed)> writes:
>>Can you please tell me a way in which I can display a number with
>>leading 0's.

> Well, if you interpret the number as a String
> (e.g., Integer.toString()), and remember that the
> String has a length(), you can calculate the number of
> leading 0's you need make the total length() == 5.


Or let the computer calculate that for you:
String tmp = "0000" + number; // more 00 harmless
return tmp.substring (tmp.length - 5);
 
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
Re: Using %x to format number to hex and number of digits Tim Chase Python 2 11-06-2010 01:22 AM
Re: Using %x to format number to hex and number of digits Chris Rebert Python 1 11-05-2010 07:05 PM
binary number format ? format character %b or similar. Ken Starks Python 4 06-23-2008 08:59 AM
converting exponential format number to decimal format number Fei Liu Perl Misc 21 12-16-2006 01:49 AM
XSLT error with number and format-number functions silellak@gmail.com XML 1 09-18-2006 11:08 PM



Advertisments