Java Reference
In-Depth Information
0
,
1
,
22
d
/
7
,
100.2345678
};
/** The main (and only) method in this class. */
public
public static
void
main
(
String
[]
av
) {
// Get a format instance
NumberFormat form
=
NumberFormat
.
getInstance
();
static
void
// Set it to look like 999.99[99]
form
.
setMinimumIntegerDigits
(
3
);
form
.
setMinimumFractionDigits
(
2
);
form
.
setMaximumFractionDigits
(
4
);
// Now print using it.
for
for
(
int
int
i
=
0
;
i
<
data
.
length
;
i
++)
System
.
out
.
println
(
data
[
i
] +
"\tformats as "
+
form
.
format
(
data
[
i
]));
}
}
This prints the contents of the array using the
NumberFormat
instance
form
:
$
java numbers.NumFormat2
0.0 formats as 000.00
1.0 formats as 001.00
3.142857142857143 formats as 003.1429
100.2345678 formats as 100.2346
$
You can also construct a
DecimalFormat
with a particular pattern or change the pattern dy-
namically using
applyPattern()
. Some of the more common pattern characters are shown
in
Table 5-2
.