Start learning free

LESSON 23 OF 348 · CALCULATIONS WITH NUMBERS

Floor Division in Kotlin: Quotient with Int Division

Lesson Read the full explanation

TL ; DR:

  • The Math.floor() function returns the largest integer less than or equal to a given number:

    println(Math.floor(3.9)) // Output: 3
    println(Math.floor(5.2)) // Output: 5
    println(Math.floor(7.0)) // Output: 7
    
  • We can use this function if we want to compute the integral part of some division (quotient):

    val a = 3
    val b = 7
    
    println(Math.floor(20.0 / a)) // Output: 6
    println(Math.floor(b.toDouble() / a)) // Output: 2
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

Floor division

Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6

It can also be used with variables:

a = 3
b = 7

print(20 // a) # Output: 6
print(b // a) # Output: 2

Modulo

The modulo operator (%) calculates the remainder of dividing two values:

println(10 % 2) // Output: 0
println(15 % 4) // Output: 3
println(20 % 3) // Output: 2

// Can be used with variables:
val a = 2
val b = 4

println(b % a) // Output: 0
println(11 % b) // Output: 3

Quotient and remainder

In programming, we combine both these concepts to get the quotient and remainder of some divison:

// Let's divide 26 by 3:
val quotient = 26 / 3
val remainder = 26 % 3

println(quotient) // Output: 8
println(remainder) // Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Floor Division in Kotlin, explained

    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.

    1. Divide the money by the price. Both variables are Int, so amount / price is integer division, and 70 / 15 is 4.
    2. 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 / 15 is 4.666666666666667, and the fraction stays.
    • Dividing the wrong way around. price / amount is 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 Int values gives the quotient as an Int.
    • Use % for the remainder.
    • For negative numbers, / truncates toward zero; Math.floorDiv floors.

    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 Int values and non-negative results: use a / b.
    • Possible negative results where rounding down matters: use Math.floorDiv(a, b).
    • A Double you want rounded down: use Math.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.

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free