the
int
of
factor
int
(
expression )
Factor and expression yield integer
s, number
s, or complex
es.
put
int
(steve) into bill
The int
function returns the given value rounded towards zero. If the given value is an integer
, the given value is returned. Otherwise, the given value is rounded to the integer with the next lowest absolute value and the same sign. In other words, the int
function returns the integer part of the given value, discarding the fractional part, regardless of sign.
The following table illustrates the behavior of the int
function:
argument | result |
---|---|
2 | 2 |
1.75 | 1 |
1.5 | 1 |
1.25 | 1 |
1 | 1 |
0.75 | 0 |
0.5 | 0 |
0.25 | 0 |
0 | 0 |
-0.25 | 0 |
-0.5 | 0 |
-0.75 | 0 |
-1 | -1 |
-1.25 | -1 |
-1.5 | -1 |
-1.75 | -1 |
-2 | -2 |
If the given value is a complex
, the real and imaginary parts of the complex number are each rounded individually.
The following table shows the correspondences among the various roundingMethods, rounding functions, and division operators:
rounding parameter |
function | operator | java.math.RoundingMode |
---|---|---|---|
to ceiling |
ceil |
CEILING |
|
to floor |
floor |
div / mod |
FLOOR |
up / to infinity |
aug |
UP |
|
down / to zero |
trunc |
quot / rem |
DOWN |
to nearest |
round |
HALF_UP |
|
to even |
rint |
HALF_EVEN |
The following table illustrates the behavior of the rounding functions side-by-side:
argument | ceil |
floor |
aug |
trunc |
round |
rint |
---|---|---|---|---|---|---|
2 | 2 | 2 | 2 | 2 | 2 | 2 |
1.75 | 2 | 1 | 2 | 1 | 2 | 2 |
1.5 | 2 | 1 | 2 | 1 | 2 | 2 |
1.25 | 2 | 1 | 2 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
0.75 | 1 | 0 | 1 | 0 | 1 | 1 |
0.5 | 1 | 0 | 1 | 0 | 1 | 0 |
0.25 | 1 | 0 | 1 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 |
-0.25 | 0 | -1 | -1 | 0 | 0 | 0 |
-0.5 | 0 | -1 | -1 | 0 | -1 | 0 |
-0.75 | 0 | -1 | -1 | 0 | -1 | -1 |
-1 | -1 | -1 | -1 | -1 | -1 | -1 |
-1.25 | -1 | -2 | -2 | -1 | -1 | -1 |
-1.5 | -1 | -2 | -2 | -1 | -2 | -2 |
-1.75 | -1 | -2 | -2 | -1 | -2 | -2 |
-2 | -2 | -2 | -2 | -2 | -2 | -2 |