Java Integer Division: How to Get a Double Result
Why 7 / 2 is 3 in Java and how to get 3.5: cast one operand to double, avoid the (double)(a / b) trap, and handle rounding, negatives and zero.
When both operands are int (or long), Java performs integer division and throws away the fractional part, so 7 / 2 is 3. To get a double result, make at least one operand a double before the division happens:
int a = 7;
int b = 2;
System.out.println(a / b); // 3 (integer division)
System.out.println((double) a / b); // 3.5 (double division)Why does 7 / 2 equal 3 in Java?
The type of an arithmetic expression is decided by its operands, not by the variable you store the result in. If both sides of / are integers, the result is an integer, and Java truncates toward zero: it simply drops everything after the decimal point.
System.out.println(7 / 2); // 3
System.out.println(-7 / 2); // -3 (toward zero, not -4)
System.out.println(1 / 3); // 0
double r = 7 / 2; // division happens first, in int
System.out.println(r); // 3.0The last example is the most common surprise: assigning to a double variable does not help, because 7 / 2 has already produced 3 by the time the value is widened to 3.0.
Ways to get a double result
Any of these turn the division into floating-point division:
int a = 7, b = 2;
double d1 = (double) a / b; // 3.5 - cast one operand
double d2 = a * 1.0 / b; // 3.5 - multiply by a double literal first
double d3 = a / 2.0; // 3.5 - use a double literal
double d4 = 7.0 / b; // 3.5Casting with (double) is the clearest choice when both values are variables. See Java type casting for how widening and narrowing conversions work.
The (double)(a / b) trap
Where you put the parentheses matters. A cast binds tighter than /, so (double) a / b casts a first. Wrapping the whole division casts the already truncated result:
int a = 7, b = 2;
System.out.println((double) a / b); // 3.5 - correct
System.out.println((double) (a / b)); // 3.0 - wrong: a / b is already 3The same applies to longer expressions, which are evaluated left to right: 1 / 3 * 3.0 gives 0.0 because 1 / 3 is computed in int first, while 1.0 / 3 * 3 gives 1.0.
Can you divide a double by an int (or an int by a double)?
Yes. When one operand is double and the other is int, Java automatically promotes the int to double (binary numeric promotion), and the result is a double:
double x = 10;
int y = 4;
System.out.println(x / y); // 2.5
System.out.println(5 / 2.0); // 2.5
System.out.println(5.0 / 2); // 2.5The general rule: if either operand is double, the result is double; otherwise if either is float, it is float; otherwise if either is long, it is long; otherwise it is int (byte, short and char are promoted to int). More on this in Java arithmetic operators.
Getting the remainder and floor division
If you actually want integer division, % gives you the remainder. Its result takes the sign of the left operand (the dividend), which is consistent with / truncating toward zero:
System.out.println(7 % 2); // 1
System.out.println(-7 % 2); // -1
System.out.println(Math.floorDiv(-7, 2)); // -4 (rounds toward negative infinity)
System.out.println(Math.floorMod(-7, 2)); // 1Use Math.floorDiv and Math.floorMod when negative numbers must round down, for example when bucketing values or wrapping indexes.
Rounding the double result
Double division often produces long fractions. Round only when you display or store the value:
double avg = (double) 10 / 3;
System.out.println(avg); // 3.3333333333333335
System.out.println(Math.round(avg * 100) / 100.0); // 3.33 (as a double)
System.out.println(String.format("%.2f", avg)); // 3.33 (as a String)Note that String.format uses the default locale, so on some systems it prints 3,33. For money, avoid double altogether and use BigDecimal with an explicit scale and rounding mode:
import java.math.BigDecimal;
import java.math.RoundingMode;
BigDecimal total = new BigDecimal("10.00");
BigDecimal share = total.divide(new BigDecimal("3"), 2, RoundingMode.HALF_UP);
System.out.println(share); // 3.33Calling divide without a scale on a non-terminating result such as 10 / 3 throws an ArithmeticException. The Java Math class chapter covers Math.round, Math.floor and Math.ceil in more detail.
Division by zero: int vs double
Integer and floating-point division behave very differently when the divisor is zero:
System.out.println(5.0 / 0); // Infinity
System.out.println(-5.0 / 0); // -Infinity
System.out.println(0.0 / 0); // NaN
System.out.println(5 / 0); // throws ArithmeticException: / by zeroInteger division by zero throws an exception, while double division returns Infinity or NaN silently. If you switch to double division, check the divisor yourself (or test the result with Double.isInfinite / Double.isNaN) so bad values do not spread through later calculations.
Quick reference
| Expression | Result | Why |
|---|---|---|
7 / 2 | 3 | both int, truncated |
-7 / 2 | -3 | truncates toward zero |
(double) 7 / 2 | 3.5 | cast happens before division |
(double) (7 / 2) | 3.0 | cast happens after truncation |
7 * 1.0 / 2 | 3.5 | 7 * 1.0 is already a double |
7 % 2 | 1 | remainder |
Math.floorDiv(-7, 2) | -4 | rounds toward negative infinity |
5 / 0 | exception | ArithmeticException |
5.0 / 0 | Infinity | IEEE 754 floating point |