Quotient and remainder
Dividing 20 by 3 can be read in two ways. The result is 6.666..., but the school way is to ask how many times 3 fits completely into 20 and what is left. The number of complete fits is the quotient, and what is left over is the remainder:
10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2
Programs often need only the quotient: how many full boxes, how many full hours, how many whole products can be bought.
Int division gives the quotient
Kotlin has no separate floor division operator. The ordinary / already does the job when both operands are integers: the result is an Int and the fractional part is discarded.
println(10 / 2) // 5
println(15 / 4) // 3
println(20 / 3) // 6
The remainder comes from %, so a division can be split into its two parts:
val quotient = 26 / 3
val remainder = 26 % 3
println(quotient) // 8
println(remainder) // 2
These always fit together: quotient * 3 + remainder is 26 again.
Truncation is not always flooring
Strictly, "floor" means rounding down toward negative infinity. Kotlin's / on integers truncates toward zero. For positive numbers the two are the same. They differ for negative results:
println(-7 / 2) // -3
println(Math.floorDiv(-7, 2)) // -4
println(Math.floorMod(-7, 3)) // 2
println(-7 % 3) // -1
Plain -7 / 2 gives -3, while Math.floorDiv(-7, 2) gives -4, the true floor. When you need a floor division with negative numbers, use Math.floorDiv.
Math.floor works on Double values
Math.floor takes a Double and returns the largest whole number not above it, but it returns that as a Double, so the output has a decimal point:
println(Math.floor(3.9)) // 3.0
println(Math.floor(20.0 / 3)) // 6.0
println(Math.floor(-3.5)) // -4.0
It is useful when you already have a Double, but for two integers, / is shorter and keeps the result an Int.
When to use the quotient
The quotient answers "how many whole ones fit?". How many full boxes of 12 can 100 eggs fill? 100 / 12 is 8. How many whole hours are in 200 minutes? 200 / 60 is 3. Pair it with % and you also get the leftover: 4 eggs, or 20 minutes. This quotient-and-remainder pair appears in many later problems, such as converting units, splitting items into groups and working with digits.
The exercise
A product costs price dollars (15) and Andy has amount dollars (70). Since he can't buy a fraction of a product, the answer is the number of whole products, the quotient of amount divided by price.
- Divide the money by the price. Both variables are
Int, soamount / priceis integer division, and 70 / 15 is 4. - Print the result. There is no extra rounding to do, because the fractional part, the 0.67 of a product, is already dropped.
val amount: Int = 70
val price: Int = 15
println(amount / price)
The program prints 4. Four products cost 60, and Andy keeps 10.
Common mistakes
- Making one value a
Double.70.0 / 15is 4.666666666666667, and the fraction stays. - Dividing the wrong way around.
price / amountis 0. - Dividing by zero. An integer division by zero throws an
ArithmeticException. - Expecting rounding.
/never rounds up: 14 / 15 is 0, not 1.
Key takeaways
- Dividing two
Intvalues gives the quotient as anInt. - Use
%for the remainder. - For negative numbers,
/truncates toward zero;Math.floorDivfloors.
Converting between Int and Double
You can reach a whole number from a Double too. The toInt() function drops the fractional part, which truncates toward zero just like integer division does. 7.9.toInt() is 7 and (-7.9).toInt() is -7, while Math.floor(-7.9).toInt() is -8. Starting from integers, 7 / 2 is 3 and 7 / 2.0 is 3.5; converting afterwards with (7 / 2).toDouble() only turns the already-truncated 3 into 3.0.
Choosing between the tools
- Two
Intvalues and non-negative results: usea / b. - Possible negative results where rounding down matters: use
Math.floorDiv(a, b). - A
Doubleyou want rounded down: useMath.floor(x), and call.toInt()if you need an integer afterwards.
For the lesson's positive amounts, all three tools agree, so the simplest one, / on two integers, is the right choice.