Forums Register Login

Java 1.5 String.format

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
See API for java.util.Formatter...
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

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

%[argument_index$][flags][width][.precision]conversion

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 ]
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12670 times.
Similar Threads
Decimals not converting to correct fractions
How to retain decimal precisions after converting from String to double
NumberFormatting
format decimal numbers
'Implied Decimal' format in Java for Double/Float
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:17:33.