Java Reference
In-Depth Information
zero will be returned. When arguments that are positive or negative infinity are passed
into
round()
, a result equal to the value of
Integer.MAX_VALUE
or
In-
teger.MIN_VALUE
, respectively, will be returned. The second version of the
java.lang.Math round()
method accepts a
double
value. The
double
value
is rounded to the closest
long
value, with ties rounding up. Just like the other
round()
, if the argument is Not a Number (
NaN
), a zero will be returned. Similarly,
when arguments that are positive or negative infinity are passed into
round()
, a res-
ult equal to the value of
Long.MAX_VALUE
or
Long.MIN_VALUE
, respectively,
will be returned.
Note
NaN
,
POSITIVE_INFINITY
, and
NEGATIVE_INFINITY
are constant
values defined within the
Float
and
Double
classes.
NaN
(Not a Number) is an un-
defined or unrepresentable value. For example, a
NaN
value can be produced by divid-
ing
0.0f
by
0.0f
. The values represented by
POSITIVE_INFINITY
and
NEGATIVE_INFINITY
refer to values that are produced by operations that generate
such extremely large or negative values of a particular type (floating-point or double)
that they cannot be represented normally. For instance, 1.0/0.0 or -1.0/0.0 would pro-
duce such values.
4-2. Formatting Double and Long Decim-
al Values
Problem
You need to be able to format
double
and
long
numbers in your application.
Solution
Use the
DecimalFormat
class to format and round the value to the precision your
application requires. In the following method, a
double
value is accepted and a
formatted string value is printed: