Results 1 to 4 of 4
  1. #1
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default Lining Up Numbers

    How can I line up numbers, so that the decimal points all form a column?

    I'm trying to generate a report of fines collected per month, appending lines to a JTextArea, and using
    Java Code:
    	page.append(String.format("\t%-7tB\t%7.2f\n", date, monthlyFines));
    (date is a Date and monthlyFines is a float).
    And it pretty much does what I want, but it looks like all of the numbers are centered geometrically, so the decimal point on "23.00" is shifted to the right a little compared with "3.00". The left edges of the month names are all lined up, which is what I wanted.

    I would actually like to put a dollar sign near the number for the first month, but not any others, without screwing up the alignment. Is there an easy way to do that?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    works for me...

    perhaps your issue is with your font not having uniform width?

  3. #3
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default

    A non-uniform font has caused me consternation in other ways. I'd like to be able to say "Go two inches from the edge regardless of how many characters you can cram in there." But here's an excerpt from another report that I've just created.

    $ 10.04: #101 Hansen, Gregory
    $ 6.60: #103 Low, Justin
    $ 3.60: #121 Hyde, Jekyll
    $ 3.30: #120 Dunkelbrotchen, Gerhardt
    $ 0.40: #119 Hoppenpopper, Bradford

    Assuming it shows through the message board, you can see how all the decimal points are lined up except that top one, which is shifted a little to the right, along with the colon, as if the numbers are being lined up by their geometric center of mass. And pushed out the rest of the text, too. I was hoping a format string like %6.2f would allocate a fixed width, but apparently it doesn't.

  4. #4
    glhansen is offline Member
    Join Date
    Mar 2009
    Posts
    20
    Rep Power
    0

    Default

    For what it matters, the reply as I see it above doesn't look like the reply as I saw it in the Quick Reply window (or in my program). Above, all the numbers seem left-justified. In my program and the Quick Reply window it was as I've described before, with the 10.04 not shifted to the right quite that much, and the "1" in "10.04" was a little to the left of the 6, the 3's, and the 0.

Similar Threads

  1. Sorting 10 numbers
    By perito in forum New To Java
    Replies: 56
    Last Post: 03-05-2009, 01:52 AM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •