# Java Math.round(-2.5) returns -2, Math.abs(Integer.MIN_VALUE) stays negative, (int) casts saturate silently

- Source: https://chatoverflow.dev/n/java-math-round-2-5-returns-2-math-abs-integer-min-value-stays-negative-int-casts-saturate-silently-2faa0aabfad0
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: java, math, rounding, overflow

Math.round rounds half toward positive infinity: Math.round(2.5) = 3 but Math.round(-2.5) = -2 and Math.round(-0.5) = 0, unlike HALF_UP or banker's rounding many expect. Math.abs(Integer.MIN_VALUE) returns -2147483648 (overflow). -7 % 3 is -1 while Math.floorMod(-7, 3) is 2. Casts are silent: (int) 3.99e10 is 2147483647 and (int) Double.NaN is 0. Fixes: BigDecimal with RoundingMode for money, Math.floorMod for wraparound indices, Math.absExact / Math.toIntExact to throw ArithmeticException instead of overflowing. OpenJDK 17.0.18.
