Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java Math.round(), floor() and ceil()

Reference for Java rounding: Math.round, floor, ceil and rint, how halves and negative numbers are treated, return types, and rounding to decimals.

Java offers four basic ways to turn a floating-point number into a whole number. Math.floor() rounds toward negative infinity, Math.ceil() rounds toward positive infinity, Math.round() rounds to the nearest integer with halves going up, and Math.rint() rounds to the nearest integer with halves going to the even neighbor.

The return types differ and are a frequent source of compile errors. floor, ceil and rint return a double (that merely holds a whole value), while round returns a long for a double argument and an int for a float argument.

Java
long r = Math.round(2.5);      // 3
double f = Math.floor(-2.5);   // -3.0
double c = Math.ceil(-2.5);    // -2.0

Try it

Java Comparing the four methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Rounding to decimals and to multiples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static long   round(double a)
static int    round(float a)
static double floor(double a)
static double ceil(double a)
static double rint(double a)
Method Rule Example (-2.5)
floor Largest whole value not greater than the argument -3.0
ceil Smallest whole value not less than the argument -2.0
round Nearest integer; ties go toward positive infinity -2
rint Nearest integer; ties go to the even value -2.0

Parameters: a is the value to round.

Returns: the rounded value, typed as shown above. None of these methods throws.

Usage notes

  • round is floor(x + 0.5) in spirit. Ties go up, so Math.round(-2.5) is -2 but Math.round(2.5) is 3. This differs from the "away from zero" rule many people expect for negative numbers.
  • Special values. round(NaN) is 0. Arguments beyond the long range clamp to Long.MAX_VALUE or Long.MIN_VALUE; for the float overload, Integer.MAX_VALUE and Integer.MIN_VALUE. floor, ceil and rint pass NaN, infinities and zero through unchanged.
  • Integer division happens first. Math.round(7 / 2) rounds 3, because 7 / 2 is already an int. Make one operand a double when you want a fractional quotient.
  • Rounding to decimals. Math.round(x * 100) / 100.0 works for display-level code but inherits binary floating-point error. Prefer String.format("%.2f", x) for output and BigDecimal with an explicit RoundingMode for money.
  • Dividing with ceiling. (int) Math.ceil((double) a / b) works, but Math.ceilDiv(a, b) (Java 18) is exact and avoids the double detour for large long values.
  • Narrowing needs a cast. int n = Math.round(2.5); does not compile, because the result is a long. Use (int) Math.round(x) or Math.toIntExact(Math.round(x)) to fail loudly on overflow.
  • Banker's rounding. rint and RoundingMode.HALF_EVEN avoid the statistical bias of always rounding ties upward.
  • Negative zero. Math.ceil(-0.4) returns -0.0, which prints as -0.0.