left-padding ints

Discussion in 'Java' started by Ike, Sep 7, 2005.

  1. Ike

    Ike Guest

    Is there something in the API for left-padding an int with, say, 0's when
    converting to a String, such that, say 1 becomes "01" ? -Ike
     
    Ike, Sep 7, 2005
    #1
    1. Advertising

  2. Ike

    Chris Smith Guest

    Ike <> wrote:
    > Is there something in the API for left-padding an int with, say, 0's when
    > converting to a String, such that, say 1 becomes "01" ? -Ike


    Yep. See java.text.DecimalFormat or java.text.NumberFormat. For
    DecimalFormat, simply use '0' characters instead of '#' characters in
    the pattern. For NumberFormat, use setMinimumIntegerDigits. The
    difference between the two is that NumberFormat is more flexible. If
    you're sure that you want to display the result in a decimal number
    system regardless of locale, then use DecimalFormat instead.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 7, 2005
    #2
    1. Advertising

  3. Ike

    Roedy Green Guest

    On Wed, 07 Sep 2005 13:48:02 GMT, "Ike" <> wrote or
    quoted :

    >Is there something in the API for left-padding an int with, say, 0's when
    >converting to a String, such that, say 1 becomes "01" ? -Ike


    make a slight mod to this code:

    /**
    * Pads the string out to the given length by applying blanks on
    the left.
    *
    * @param s
    * String to be padded/chopped.
    * @param newLen
    * length of new String desired.
    * @param chop
    * true if Strings longer than newLen should be truncated
    to newLen
    * chars.
    * @return String padded on left/chopped to the desired length.
    */
    public final static String leftPad ( String s, int newLen, boolean
    chop )
    {
    int grow = newLen - s.length();
    if ( grow <= 0 )
    {
    if ( chop )
    {
    return s.substring( 0, newLen );
    }
    else
    {
    return s;
    }
    }
    else if ( grow <= 30 )
    {
    return " ".substring( 0, grow
    ) + s;
    }
    else
    {
    return rep( ' ', grow ) + s;
    }
    } // end leftPad

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 7, 2005
    #3
  4. Ike

    Joan Guest

    "Ike" <> wrote in message
    news:mcCTe.2144$...
    > Is there something in the API for left-padding an int with,
    > say, 0's when
    > converting to a String, such that, say 1 becomes "01" ? -Ike


    // requires java 5.0
    // not tested
    int x = 5;
    String s = String.format("%02d", x); // zero not oh
     
    Joan, Sep 8, 2005
    #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. Dave
    Replies:
    7
    Views:
    5,669
    Joe Smith
    Jul 22, 2004
  2. RA
    Replies:
    1
    Views:
    376
  3. chlori

    padding-left

    chlori, Jul 5, 2003, in forum: HTML
    Replies:
    6
    Views:
    3,474
    Jacqui or (maybe) Pete
    Jul 6, 2003
  4. Replies:
    3
    Views:
    566
    Mark P
    Apr 3, 2005
  5. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    836
    Jack Klein
    Jul 10, 2004
Loading...

Share This Page