This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java 1.5 String.format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java 1.5 String.format" Watch "Java 1.5 String.format" New topic

Java 1.5 String.format

R Jarman

Joined: Feb 08, 2005
Posts: 27
Does anyone have any experience with the new format method in the String class in 1.5. I have several decimal numbers (money) and need them to align on the decimal point in columns. I.E.

12.45 11.23 3.90
2.34 23.90 34.53

I'm hoping there is a format string I can pass to do this rather than figuring out how long each number is in characters to calculate the number of spaces between each number. The numbers need to be in this format to be used in another application. Any help would be appreciated.
marc weber

Joined: Aug 31, 2004
Posts: 11343

See API for java.util.Formatter...

In general, a format string (the first argument to String.format) for numeric material has the syntax:


The bracketed components are all optional, so at a minimum we need the percent sign followed by the appropriate conversion character. In this case, we're dealing with floating point numbers, so we probably want a conversion character of f. Therefore, our format string is at least "%f".

Now, let's customize using the optional components...

First, we'll want each of these to show 2 digits to the right of the decimal, so we'll add the [.precision] component. (Note the decimal point that flags this as a precision indicator.) Basically, this inserts trailing zeros or rounds as needed. So inserting ".2" gives us a format string of "%.2f".

Now, we also want to align on the decimal. We've already ensured that there are exactly two digits to the right of the decimal, so what we really want is to right-justify within a column. To do this, we only need to specify a maximum width using the [width] component, because the default is to right-justify within that width. Twelve spaces should ensure ample room for illustration purposes, so inserting a width component of "12" gives us a format string of "%12.2f".

Finally, we'll add a flag component to include a locale-specific grouping separator (e.g., a comma separating thousands). This gives us a format string of "%,12.2f".

The following code demonstrates the result, using an array of doubles as test data...

Note that each output String is 12 characters wide, with leading spaces inserted so that the number is right-justified within this width. The number itself has been rounded to 2 decimal places (with trailing zeros as needed) and is displayed with a comma separator (as needed).

(Also note the new printf method in PrintStream, which could further streamline the above code.)
[ February 09, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Java 1.5 String.format
It's not a secret anymore!