Number format (ex: 5 as 00005)

Discussion in 'Java' started by m, Nov 9, 2006.

  1. m

    m Guest

    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
     
    m, Nov 9, 2006
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 9, 2006
    #2
    1. Advertising

  3. m

    djthomp Guest

    On Nov 9, 3:04 pm, "m" <> 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...tml#format(java.lang.String, java.lang.Object...)
     
    djthomp, Nov 9, 2006
    #3
  4. m

    djthomp Guest

    djthomp, Nov 9, 2006
    #4
  5. "m" <> 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
     
    Mark Jeffcoat, Nov 9, 2006
    #5
  6. m

    m Guest

    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.
     
    m, Nov 9, 2006
    #6
  7. m

    m Guest

    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);

    }

    }
     
    m, Nov 9, 2006
    #7
  8. 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 9, 2006
    #8
  9. m

    Paul Hamaker Guest

    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
     
    Paul Hamaker, Nov 9, 2006
    #9
  10. m

    Robert Mabee Guest

    Mark Jeffcoat wrote:
    > "m" <> 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);
     
    Robert Mabee, Nov 10, 2006
    #10
  11. m 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
    >


    Although all the suggested methods work for the particular case of 1
    through 99999, in general I would avoid the manual methods and stick
    with using either printf or a DecimalFormat.

    Both automate reasonable handling of more general cases, such as
    negative numbers and numbers that are greater than 99999, without
    dropping information. You are likely to need to use the supplied methods
    for some cases, so why not learn and use them even when you could do it
    manually without too many special cases?

    Patricia
     
    Patricia Shanahan, Nov 10, 2006
    #11
  12. m

    trippy Guest

    In article <>, m
    took the hamburger meat, threw it on the grill, and I said "Oh Wow"...

    > 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
    >
    >


    public String pimpMyNumber(int aNumber) {

    DecimalFormat df = new DecimalFormat("000,000");
    StringBuffer sb = df.format(aNumber);
    return sb.toString();

    }

    --
    trippy
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

    NP: "All I Really Want" -- Alanis Morissette

    "Now, technology's getting better all the time and that's fine,
    but most of the time all you need is a stick of gum, a pocketknife,
    and a smile."

    -- Robert Redford "Spy Game"
     
    trippy, Nov 10, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    714
    Joe Kesselman
    Sep 19, 2006
  2. Ken Starks
    Replies:
    4
    Views:
    357
    Ken Starks
    Jun 23, 2008
  3. Chris Rebert
    Replies:
    1
    Views:
    406
    Grant Edwards
    Nov 5, 2010
  4. Tim Chase
    Replies:
    2
    Views:
    577
    Tim Chase
    Nov 6, 2010
  5. Fei Liu
    Replies:
    21
    Views:
    2,421
    John Bokma
    Dec 16, 2006
Loading...

Share This Page