Java: String Justification & Number Formatting

Discussion in 'Mac Programming' started by toddburch, Apr 13, 2007.

  1. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #1
    Left, right, center justification. Seems like a basic feature, but I can't seem to find it. Must be buried in some obscure package. Any ideas?

    Also, number formatting. Let's say I want to format numbers 1 through 100 with leading blanks or zeros in a report to keep everything lined up nice and tidy. How to?

    Thanks, Todd
     
  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
  3. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #3
    Linking to the 1.4.2 API Doctor Q? For shame... :p
     
  4. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    Aren't they all equally good for this type of documentation? I just picked at random.
     
  5. toddburch thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    Thanks DrQ. I will look into these.

    I guess I'm spoiled with Ruby string handling capabilities, and REXX too.

    Todd
     
  6. tilman macrumors regular

    Joined:
    Feb 28, 2006
    #6
    If you are using Java 1.5 or newer, you can use the newly introduced formatters:
    String.format("%1$-20s %2$-20s %3$6d %4$9.2f", name, operation, getCalls(), getTime());
     
  7. toddburch thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    String.format works perfectly. Not sure how I missed that - it's right there in the doc! Duh!

    Thanks, Todd
     

Share This Page