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.
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
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.
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
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
roundis floor(x + 0.5) in spirit. Ties go up, soMath.round(-2.5)is-2butMath.round(2.5)is3. This differs from the "away from zero" rule many people expect for negative numbers.- Special values.
round(NaN)is0. Arguments beyond thelongrange clamp toLong.MAX_VALUEorLong.MIN_VALUE; for thefloatoverload,Integer.MAX_VALUEandInteger.MIN_VALUE.floor,ceilandrintpassNaN, infinities and zero through unchanged. - Integer division happens first.
Math.round(7 / 2)rounds3, because7 / 2is already anint. Make one operand adoublewhen you want a fractional quotient. - Rounding to decimals.
Math.round(x * 100) / 100.0works for display-level code but inherits binary floating-point error. PreferString.format("%.2f", x)for output andBigDecimalwith an explicitRoundingModefor money. - Dividing with ceiling.
(int) Math.ceil((double) a / b)works, butMath.ceilDiv(a, b)(Java 18) is exact and avoids the double detour for largelongvalues. - Narrowing needs a cast.
int n = Math.round(2.5);does not compile, because the result is along. Use(int) Math.round(x)orMath.toIntExact(Math.round(x))to fail loudly on overflow. - Banker's rounding.
rintandRoundingMode.HALF_EVENavoid the statistical bias of always rounding ties upward. - Negative zero.
Math.ceil(-0.4)returns-0.0, which prints as-0.0.
Related
Math,Math.random(),Math.addExact(),BigDecimal- Lessons: Math, Big numbers, Type casting