Java Math rint() Method
Example
Round numbers to the nearest integer:
System.out.println(Math.rint(0.5));
System.out.println(Math.rint(1.5));
System.out.println(Math.rint(5));
System.out.println(Math.rint(5.1));
System.out.println(Math.rint(-5.1));
System.out.println(Math.rint(-5.9));
Definition and Usage
The rint()
method rounds a number to the nearest integer. If there are two integers that are equally close to the number then the even integer will be returned.
Note: This method is very similar to round()
. The main differences between rint()
and round()
are:
round()
returnslong
orint
data types whilerint()
returns adouble
.- When the decimal part of the number is exactly 0.5,
rint()
returns the nearest even integer whileround()
returns the highest of the two nearest integers
Syntax
public static double rint(double number)
Parameter Values
Parameter | Description |
---|---|
number | Required. A number to round. |
Technical Details
Returns: | A double value representing the nearest integer to a number. |
---|---|
Java version: | Any |
❮ Math Methods