Formatting an integer with leading zeros

Discussion in 'Java' started by GIMME, Feb 11, 2004.

  1. GIMME

    GIMME Guest

    Is there a "java" way to format int values as strings with
    leading zeros?

    I've reviewed many posts and the solutions involved rolling
    your own with prepending a "0" as necessary until getting
    the desired result.

    A far cry from C's %5d

    Thanks
     
    GIMME, Feb 11, 2004
    #1
    1. Advertising

  2. GIMME

    Anton Spaans Guest

    "GIMME" <> wrote in message
    news:...
    > Is there a "java" way to format int values as strings with
    > leading zeros?
    >
    > I've reviewed many posts and the solutions involved rolling
    > your own with prepending a "0" as necessary until getting
    > the desired result.
    >
    > A far cry from C's %5d
    >
    > Thanks


    Not necessary. Here is a sample that always prints at least 4 digits
    (including leading zeros):

    java.text.DecimalFormat nft = new
    java.text.DecimalFormat("#0000.###");
    nft.setDecimalSeparatorAlwaysShown(false);
    System.out.println(nft.format(20));

    -- Anton Spaans
     
    Anton Spaans, Feb 11, 2004
    #2
    1. Advertising

  3. "GIMME" <> skrev i en meddelelse
    news:...
    > Is there a "java" way to format int values as strings with
    > leading zeros?
    >
    > I've reviewed many posts and the solutions involved rolling
    > your own with prepending a "0" as necessary until getting
    > the desired result.

    Take a look at java.text.DecimalFormat - e.g.

    myFormat = new java.text.DecimalFormat("00000");
    myFormat.format(new Integer(someValue));

    There are many other formatting chars as well.

    >
    > A far cry from C's %5d


    >
    > Thanks
     
    Niels Ull Harremoës, Feb 11, 2004
    #3
  4. GIMME

    nos Guest

    "GIMME" <> wrote in message
    news:...
    > Is there a "java" way to format int values as strings with
    > leading zeros?
    >
    > I've reviewed many posts and the solutions involved rolling
    > your own with prepending a "0" as necessary until getting
    > the desired result.
    >
    > A far cry from C's %5d
    >
    > Thanks


    But is it a far cry from COBOL?
     
    nos, Feb 12, 2004
    #4
  5. GIMME

    Sudsy Guest

    nos wrote:
    > "GIMME" <> wrote in message

    <snip>
    >>A far cry from C's %5d
    >>
    >>Thanks

    >
    >
    > But is it a far cry from COBOL?


    You mean PICT X(5)?
     
    Sudsy, Feb 12, 2004
    #5
  6. GIMME

    Dale King Guest

    "GIMME" <> wrote in message
    news:...
    > Is there a "java" way to format int values as strings with
    > leading zeros?
    >
    > I've reviewed many posts and the solutions involved rolling
    > your own with prepending a "0" as necessary until getting
    > the desired result.
    >
    > A far cry from C's %5d



    Note in JDK1.5 you will get a Java equivalent of printf.
    --
    Dale King
     
    Dale King, Feb 13, 2004
    #6
    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. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,051
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,249
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. Stian
    Replies:
    2
    Views:
    614
    Thomas D'Tak
    Sep 19, 2004
  4. hssig
    Replies:
    7
    Views:
    1,402
    Jonathan Bromley
    Sep 29, 2010
  5. Tom Reinhart

    Formatting date without leading zeros

    Tom Reinhart, May 15, 2005, in forum: Ruby
    Replies:
    10
    Views:
    294
    tony summerfelt
    May 15, 2005
Loading...

Share This Page