Start learning free

LESSON 23 OF 414 · CALCULATIONS WITH NUMBERS

Floor Division in Scala: Quotient with Int Division

Lesson Read the full explanation

TL ; DR:

  • When both numbers are Ints, the / operator drops the fractional part and gives the quotient:

    println(10 / 2) // Output: 5
    println(15 / 4) // Output: 3
    println(20 / 3) // Output: 6
    
  • It can also be used with variables:

    val a = 3
    val b = 7
    
    println(20 / a) // Output: 6
    println(b / 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

Scala has no separate floor-division operator. When both operands are Ints, the normal division operator (/) returns the integral part of the result (the quotient):

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

It can also be used with variables:

val a = 3
val b = 7

println(20 / a) // Output: 6
println(b / a) // Output: 2

If one of the numbers is a Double, the result keeps its decimals: println(20 / 3.0) prints 6.666666666666667. Also, Math.floor works on Doubles and returns a Double, so println(Math.floor(3.9)) prints 3.0, not 3. For a whole-number quotient, use / on two Ints.


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 c = 2
val d = 4

println(d % c) // Output: 0
println(11 % d) // Output: 3

Quotient and remainder

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

// 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

    SCALA · CALCULATIONS WITH NUMBERS

    Floor Division in Scala, explained

    Quotient and remainder

    When you divide one whole number by another, the answer splits into two parts. The quotient is how many times the divisor fits completely. The remainder is what is left over:

    15 / 4 = quotient 3, remainder 3
    20 / 3 = quotient 6, remainder 2
    

    In Scala, the quotient and the remainder each have an operator. For two Int values, / gives the quotient and % gives the remainder:

    println(15 / 4)
    println(15 % 4)
    println(26 / 3)
    println(26 % 3)
    
    3
    3
    8
    2
    

    There is no separate floor-division operator like the one some other languages have. Integer division is built into /: when both operands are Int, the result is an Int and the fractional part is dropped.

    Int division against Double division

    The same digits give a different result when a Double is involved:

    println(70 / 15)
    println(70 / 15.0)
    
    4
    4.666666666666667
    

    The type of the operands chooses the behaviour. If you want the whole-number quotient, keep both sides as Int. If you need the decimals, make one side a Double.

    Truncation, flooring and negative numbers

    Int division truncates toward zero. For positive numbers that is the same as rounding down, which is what "floor" means. For negative numbers the two differ:

    println(-7 / 2)
    println(Math.floorDiv(-7, 2))
    println(-7 % 2)
    println(Math.floorMod(-7, 2))
    
    -3
    -4
    -1
    1
    

    Plain / gives -3, because it cuts off the fraction of -3.5. Math.floorDiv rounds down to -4. Scala can call this Java method directly. The matching Math.floorMod returns a remainder that is never negative for a positive divisor. For the positive values in this chapter, / is all you need.

    Math.floor is for Doubles

    Math.floor takes a Double and returns a Double. It rounds down, but it keeps the type, so the result still prints with a decimal part:

    println(Math.floor(3.9))
    println(Math.floor(-3.5))
    println(Math.floor(20 / 3))
    
    3.0
    -4.0
    6.0
    

    The last line shows a common trap. The division 20 / 3 is done on Int values first and already gives 6. Math.floor then receives 6 and returns it as 6.0. It does no work on the quotient, and it changes the type. For an integer quotient, use plain / on two Int values.

    The exercise

    A product costs price dollars and Andy has amount dollars. He cannot buy a fraction of a product, so the answer is the number of complete times the price fits into the amount. That is the quotient, and amount / price computes it directly because both values are Int. With 70 and 15, the exact result is 4.67, and the integer division drops the .67:

    val amount: Int = 70
    val price: Int = 15
    println(amount / price)
    

    The output is:

    4
    

    Andy can buy 4 products. The 10 dollars left over are the remainder, amount % price, which the next lesson covers.

    Common mistakes

    • Making an operand a Double by accident. amount / price.toDouble gives 4.666666666666667. Keep both values as Int.
    • Swapping the operands. price / amount is 15 / 70, which is 0.
    • Wrapping the result in Math.floor. It is unnecessary for Int division, and it turns the answer into 4.0.
    • A zero divisor. Dividing an Int by 0 throws java.lang.ArithmeticException: / by zero.

    Key takeaways

    • For two Int values, / is the quotient and % is the remainder.
    • The fraction is dropped toward zero; Math.floorDiv rounds down for negative numbers.
    • Math.floor returns a Double, so it is not the tool for integer quotients.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free