Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What do these print? Math.round(2.5), Math.round(-2.5), Math.rint(2.5), Math.floor(-2.1), Math.ceil(-2.1), (int) -2.9, Math.floorDiv(-7, 2), -7 / 2, Math.floorMod(-7, 3), -7 % 3

Math.round(2.5) is 3 and Math.round(-2.5) is -2, because round goes to the nearest whole number and sends exact halves toward positive infinity. Math.rint(2.5) is 2.0, because rint sends halves to the even neighbour and returns a double. Math.floor(-2.1) is -3.0, Math.ceil(-2.1) is -2.0 and (int) -2.9 is -2, because a cast truncates toward zero. -7 / 2 is -3 while Math.floorDiv(-7, 2) is -4, and -7 % 3 is -1 while Math.floorMod(-7, 3) is 2.

Why this question is asked

Rounding looks like school arithmetic, but Java has at least five different rules for it, and they only disagree on negative numbers and exact halves, which are exactly the cases tests forget. Interviewers ask this to see whether we know which direction each method goes, because using the wrong one shows up as off-by-one bugs in pagination, time buckets, prices and array indexes. In this answer, we will recap the directions a number can be rounded, then go through round and rint, floor, ceil and casts, and finally integer division and remainder, where Java's operators and the floor methods part ways.

1A Quick Recap of Rounding Directions

Every rounding method answers one question: when a number is not whole, which whole number do we pick? There are four basic directions. Floor goes down, toward negative infinity. Ceiling goes up, toward positive infinity. Truncation goes toward zero, which means down for positive numbers and up for negative ones. Nearest picks the closest whole number, and then needs an extra rule for exact halves, such as 2.5, which is equally close to 2 and 3.

For positive numbers, floor and truncation agree, which is why many people never notice the difference. Negative numbers are where the methods separate, so the question deliberately uses -2.1, -2.5, -2.9 and -7.

Every expression in the question
ExpressionResultRule
Math.round(2.5)3 (long)nearest, halves toward +infinity
Math.round(-2.5)-2 (long)nearest, halves toward +infinity
Math.rint(2.5)2.0 (double)nearest, halves to even
Math.floor(-2.1)-3.0toward -infinity
Math.ceil(-2.1)-2.0toward +infinity
(int) -2.9-2truncate toward zero
-7 / 2-3integer division truncates toward zero
Math.floorDiv(-7, 2)-4division rounded toward -infinity
-7 % 3-1sign follows the dividend
Math.floorMod(-7, 3)2sign follows the divisor
The negative cases are where the methods disagree.

2Math.round and Math.rint at Exact Halves

Both methods round to the nearest whole number, and they only differ when the fraction is exactly one half.

Java
System.out.println(Math.round(2.5)); // 3
System.out.println(Math.round(-2.5)); // -2
System.out.println(Math.round(-2.6)); // -3
System.out.println(Math.rint(2.5)); // 2.0
System.out.println(Math.rint(3.5)); // 4.0
System.out.println(Math.rint(-2.5)); // -2.0

Math.round sends halves toward positive infinity, so 2.5 goes up to 3 but -2.5 also goes up, to -2. It is often described as adding 0.5 and taking the floor, which gives the same answers for ordinary values; since Java 7 the method is specified directly as rounding to the nearest value with ties toward positive infinity, which fixed an old edge case where 0.49999999999999994 rounded to 1. Notice also the return type: round(double) returns a long, so it prints 3, not 3.0.

Math.rint sends halves to the even neighbour. 2.5 goes to 2 and 3.5 goes to 4. This is called round half even, or banker's rounding, and its advantage is that over many values it rounds up and down equally often, so sums do not drift upwards. rint returns a double, so it prints 2.0.

3Floor, Ceiling and the Cast

Next, three ways to drop the fraction of a negative number.

Java
System.out.println(Math.floor(-2.1)); // -3.0: down, toward negative infinity
System.out.println(Math.ceil(-2.1)); // -2.0: up, toward positive infinity
System.out.println((int) -2.9); // -2: toward zero
System.out.println((int) 2.9); // 2
System.out.println((int) Math.floor(-2.9)); // -3

floor and ceil return doubles, which is why they print -3.0 and -2.0. The cast (int) simply throws the fraction away, which moves negative numbers toward zero. So (int) -2.9 is -2, not -3. If we want the floor as an int, we call Math.floor and then cast, as the last line shows. Mixing these up is a common source of off-by-one errors in code that maps coordinates or timestamps into grid cells.

4Integer Division and Remainder With Negative Numbers

The last four expressions use ints. Java's / on integers truncates toward zero, just like the cast. The % operator returns the remainder that goes with that division, so its sign follows the dividend, the number on the left.

Java
System.out.println(-7 / 2); // -3: -3.5 truncated toward zero
System.out.println(Math.floorDiv(-7, 2)); // -4: -3.5 rounded down
System.out.println(-7 % 3); // -1: because -7 = 3 * (-2) + (-1)
System.out.println(Math.floorMod(-7, 3)); // 2: because -7 = 3 * (-3) + 2
int hour = -1; // one hour before midnight
System.out.println(hour % 24); // -1: not a valid hour
System.out.println(Math.floorMod(hour, 24)); // 23
Step throughChoosing an integer division method
  1. Can the dividend be negative?
    YesContinueNoPlain / and % are fine
  2. Need a bucket, index or wrap-around?
    YesUse Math.floorModNoContinue
  3. Need the result rounded down?
    YesUse Math.floorDivNoPlain / truncates toward zero

3 steps in order. Play them, or step through them one at a time.

For non-negative values all methods agree; the choice matters only for negatives.

The two pairs always fit together: a equals b times the quotient plus the remainder. With truncating division, -7 / 3 is -2, so the remainder must be -1. With floor division, the quotient is -3, so the remainder is 2. Math.floorMod, added in Java 8 with floorDiv, returns a result with the same sign as the divisor, so with a positive divisor it is always between 0 and divisor minus 1.

That is why floorMod is the right tool for wrap-around and bucket calculations. Spreading ids across 16 shards with id % 16 produces negative shard numbers for negative ids, and an array lookup with them throws ArrayIndexOutOfBoundsException. Math.floorMod(id, 16) always gives 0 to 15. The same applies to hashCode values, which are often negative, and to clock arithmetic like the hour example.

5Follow-up questions

Which rounding should you use for money, and with which API?

Use BigDecimal with an explicit RoundingMode, never Math.round on a double. Accounting and many tax rules use RoundingMode.HALF_EVEN, while some invoicing rules require HALF_UP; the business must say which. amount.setScale(2, RoundingMode.HALF_EVEN) rounds to cents exactly, because BigDecimal stores decimal digits, while a double cannot even represent most cent values exactly, so Math.round may see 1.0149999 instead of 1.015.

Why does Math.round return a long?

Because the result of rounding a double can be far larger than an int can hold. Math.round(float) returns an int, matching the float's smaller range. If we need an int from a double, Math.toIntExact(Math.round(x)) converts and fails loudly if the value is too big.

What does Math.round return for NaN or huge values?

Math.round(Double.NaN) is 0, and values beyond the long range are clamped to Long.MAX_VALUE or Long.MIN_VALUE, with no exception. That is another reason to validate input before rounding instead of trusting the result.

6Conclusion

In this answer, we saw that Java's rounding methods agree for positive non-halves and separate on negatives and exact halves. Math.round sends halves toward positive infinity, so 2.5 is 3 and -2.5 is -2; Math.rint sends halves to the even neighbour, so 2.5 is 2.0. floor goes down, ceil goes up and a cast truncates toward zero. Integer / and % truncate, so -7 / 2 is -3 and -7 % 3 is -1, while Math.floorDiv and Math.floorMod round down, giving -4 and 2, which is what bucket and wrap-around code needs.

What to say in the interviewAbout 90 seconds aloud

Open with

Math.round of 2.5 is 3, and Math.round of minus 2.5 is minus 2, because round goes to the nearest value and breaks ties toward positive infinity, so both halves go up.

Then, if they want more

It returns a long, so it prints without a decimal point. Math.rint of 2.5 is 2.0, because rint breaks ties to the even neighbour, which is banker's rounding, and it returns a double. Math.floor of minus 2.1 is minus 3.0, since floor goes toward negative infinity, and Math.ceil of minus 2.1 is minus 2.0.

Casting minus 2.9 to int gives minus 2, because a cast just drops the fraction, so it truncates toward zero. For integers, minus 7 divided by 2 is minus 3, again truncation toward zero, but Math.floorDiv of minus 7 and 2 is minus 4, because it rounds down.

And minus 7 mod 3 is minus 1, since the remainder takes the sign of the dividend, while Math.floorMod of minus 7 and 3 is 2, with the sign of the divisor. That makes floorMod the right choice for shard numbers, array indexes from hash codes and clock arithmetic, where a negative remainder would be a bug.

For money I would not use any of these on doubles; I would use BigDecimal with setScale and an explicit RoundingMode, usually HALF_EVEN or whatever the business rule says.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What do these print? Math.round(2.5), Math.round(-2.5), Math.rint(2.5)...": https://scrivio-interview-library.netlify.app/java/types-and-values/554e9e4378/