Padding for DecimalFormat?

Discussion in 'Java' started by Timo Nentwig, Jan 5, 2004.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    Is there no padding/alignment for DecimalFormat?

    d = new DecimalFormat("00000000");
    d.format(1234);

    will return "00001234". "########" will return "1234". And how do I get "
    1234"?

    Timo
    Timo Nentwig, Jan 5, 2004
    #1
    1. Advertising

  2. Timo Nentwig wrote:

    > Is there no padding/alignment for DecimalFormat?
    >
    > d = new DecimalFormat("00000000");
    > d.format(1234);
    >
    > will return "00001234". "########" will return "1234".
    > And how do I get " 1234"?


    Not without extra processing.

    NumberFormat d = new DecimalFormat("########");
    String s = d.format(1234);
    String s0 = " ".substring(s.length()) + s;
    Thomas Schodt, Jan 5, 2004
    #2
    1. Advertising

  3. Timo Nentwig

    Roedy Green Guest

    On Mon, 05 Jan 2004 13:29:39 +0000, Thomas Schodt
    <> wrote or quoted :

    >> will return "00001234". "########" will return "1234".
    > > And how do I get " 1234"?

    >
    >Not without extra processing.


    Java presumes you will display the result in a JTextField that lets
    you specify right or left justification. It generally has no built in
    features for padding with left zeroes. Misc.LZ will do it for you in
    from http://mindprod.com/products.html#BUS

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jan 5, 2004
    #3
  4. Timo Nentwig

    Tony Dahlman Guest

    Timo Nentwig wrote:
    >
    > Hi!
    >
    > Is there no padding/alignment for DecimalFormat?
    >
    > d = new DecimalFormat("00000000");
    > d.format(1234);
    >
    > will return "00001234". "########" will return "1234". And how do I get "
    > 1234"?
    >
    > Timo


    You got good answers. But I should add that a really simple way (if you're
    not pasting the result into a JTextField) is:

    http://pws.prserv.net/ad/programs/Programs.html#PaddedDecimalFormat

    Oh, and please let me know if you improve on it.

    Regards, Tony Dahlman
    ---------------------------------------
    Fax: USA 530 846-6802
    (Sorry, email abused too much)
    Tony Dahlman, Feb 12, 2004
    #4
    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. Johnny

    DecimalFormat

    Johnny, Oct 25, 2003, in forum: Java
    Replies:
    10
    Views:
    7,857
    A Dahlman
    Oct 27, 2003
  2. Cliff
    Replies:
    5
    Views:
    544
    Roedy Green
    May 12, 2004
  3. Adam Lipscombe
    Replies:
    2
    Views:
    795
    Virgil Green
    Nov 29, 2004
  4. Mark Sizzler
    Replies:
    1
    Views:
    1,430
    Roland
    Feb 21, 2005
  5. Jim Cobban
    Replies:
    6
    Views:
    1,870
    Roedy Green
    Sep 12, 2005
Loading...

Share This Page