CKTIS / Java
Math.round — rounding to an integer
Math.round chooses the nearest integer, breaking ties toward positive infinity. The result type depends on the overload.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public static long round(double a)
public static int round(float a)Math.round(double) returns long, while Math.round(float) returns int. The method returns an integer value, not text formatted to a specified number of decimal places.
It chooses the nearest integer. Ties go toward positive infinity: 2.5 produces 3, but -2.5 produces -2. This is not rounding ties consistently away from zero.
NaN produces 0. Infinities and values outside the result range are clamped to the corresponding bounds: Long.MIN_VALUE / Long.MAX_VALUE or Integer.MIN_VALUE / Integer.MAX_VALUE.
Examples
public class Main {
public static void main(String[] args) {
long positive = Math.round(2.5);
long negative = Math.round(-2.5);
int fromFloat = Math.round(2.5f);
System.out.println(positive);
System.out.println(negative);
System.out.println(fromFloat);
System.out.println(Math.round(Double.NaN));
System.out.println(Math.round(Double.POSITIVE_INFINITY));
}
}Result
3 -2 3 0 9223372036854775807
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.