Programming and Problem Solving with Java

(やまだぃちぅ) #1

690


leftmost digit. The second label shows the result of formatting a negative number. The for-
mat is the same as in the first case, but a minus sign precedes the dollar sign.
The third label is an example of formatting a number with more digits than the pattern
specifies. Notice that the pattern is expanded to fit. The separation between the decimal
point (or the rightmost digit, in the case of an integer) and the comma closest to it are used
as a guide to the placement of additional commas. In this case, the comma is three places
to the left of the decimal point, so additional commas are inserted every three places. We can’t
split up the fractional part of a number with a separator character such as a comma or
period. If you try to use a comma in the fractional part, it simply ends up being pushed to the
right end of the number. It is also interesting to compare the value in the code segment with
the value displayed—this is a perfect example of what we said earlier about the kind of
minor inaccuracies you often encounter with floating-point numbers!
The fourth label demonstrates that an integer value can be formatted to look like a float-
ing-point value. It also shows that placing a zero in the pattern forces a zero to appear in the
resulting string when there is no corresponding digit in the number.
The fifth label shows the use of thepercentformat. Notice that the value is multiplied
by 100 before it is formatted. This example also shows that when there are more digits of pre-
cision in the fractional part than in the pattern, it is not expanded to show the additional
fractional digits. The sixth label shows the application of thepercentformat to an integer.
The last two labels in the frame demonstrate the use of the pattern we associated with
accounting. When the number is nonnegative, it is formatted normally, and when it is nega-
tive, it is enclosed in parentheses. Using two different patterns separated by a semicolon sup-
presses the automatic insertion of the minus sign and allows us to use other characters to
indicate that the number is negative. As a safeguard, however, if we mistakenly use the same
pattern on both sides of the semicolon,formatignores the semicolon and reverts to using the
minus sign.
DecimalFormatgives us a powerful mechanism to format numbers in the patterns we
typically use in our programs. However, it has some limitations. For example, when printing
a dollar amount on a check, it is typical to fill the extra space around the number with as-
terisks or dashes to prevent tampering.DecimalFormatdoesn’t enable us to do this directly.
However, because the formatmethod returns its value as a string, we can store a formatted
number into a Stringobject and then use string operations to further refine its formatting.
Suppose we write the assignments

dollar = newDecimalFormat("###,##0.00");
value = 8239.41;

then the expression

dollar.format(value)

has the value "8,239.41". Further suppose that we want to display this in a fixed space of thir-
teen character positions, where the first character is the dollar sign and the spaces between
the dollar sign and the first digit are filled with stars: "$****8,239.41". If the number has

T


E


A


M


F


L


Y


Team-Fly®

Free download pdf