Truncation vs Rounding in Java double to int Cast
Fast recall
Quick revision
Core answer
double value 9.99 to int gives 9: Java removes the fractional part toward zero; it does not round to the nearest integer. Special boundaries matter too: NaN becomes 0, and values beyond the int range clamp to Integer.MIN_VALUE or Integer.MAX_VALUE. Use Math.round, floor, ceil, or decimal rounding when those are the intended rules.A classic output-prediction gotcha — truncation vs rounding surprises many freshers
Expecting (int) 9.9 to give 10 — it gives 9; truncation always goes toward zero
Mention Math.round() for nearest-integer rounding, and Math.ceil()/Math.floor() for ceiling/floor
Interview-ready guide
Interview answer
Casting 9.99 to int returns 9 because Java truncates the finite in-range value toward zero; it does not round to the nearest integer. For example, (int) -3.7 is -3, whereas Math.floor(-3.7) is -4.
If nearest-integer behavior is required, I use Math.round and then handle its return type and range deliberately. floor and ceil express directional rules. The cast also has defined edge cases: NaN becomes zero and a value outside int range clamps to the nearest int endpoint. For financial amounts, I avoid binary floating-point rounding guesses and use BigDecimal with the required rounding mode.
Truncation vs Rounding in Java double to int Cast
Direct Answer
Casting the finite in-range double value 9.99 to int gives 9: Java removes the fractional part toward zero; it does not round to the nearest integer. Special boundaries matter too: NaN becomes 0, and values beyond the int range clamp to Integer.MIN_VALUE or Integer.MAX_VALUE. Use Math.round, floor, ceil, or decimal rounding when those are the intended rules.
Code Example
Seeing all four behaviors in one place makes the differences click — the same two numbers run through a plain cast, Math.round, Math.ceil, and Math.floor so you can compare the outputs directly.
public class DoubleTruncationDemo {
public static void main(String[] args) {
System.out.println((int) 9.99); // 9 — truncates
System.out.println((int) -3.7); // -3 — toward zero, not -4
System.out.println((int) Math.round(9.99)); // 10 — nearest
System.out.println((int) Math.round(-3.7)); // -4 — nearest
System.out.println((int) Math.ceil(9.1)); // 10 — round up
System.out.println((int) Math.floor(9.9)); // 9 — round down
}
}
The pair to stare at is (int) -3.7 giving -3 while Math.floor(-3.7) gives -4 — that single divergence between truncation and floor on negatives is the exact trap interviewers set.
Common Mistakes & Pitfalls
- Expecting
(int) 9.99to become 10: casting is not nearest rounding. - Calling truncation round-down: for negative values it moves toward zero, unlike floor.
- Assuming every narrowing overflow wraps: floating-to-integer conversion clamps after truncation; integer-to-integer narrowing follows different rules.
- Ignoring Math.round's return type: the double overload returns long, so a further checked narrowing may be needed.
- Explaining the rule as a CPU-speed choice: Java defines the result independently of a particular processor instruction.
Check your understanding
Answer these without looking back at the article.
- What does Math.round(3.5) return?
- What is the difference between Math.ceil() and Math.floor()?
- What does (int) -2.9 evaluate to?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.