Start learning free

LESSON 23 OF 415 · CALCULATIONS WITH NUMBERS

Integer Division and floor() in Swift: Quotient and Remainder

Lesson Read the full explanation

TL ; DR:

  • The floor() function (from Foundation) returns the largest integer less than or equal to a given number, as a Double:

    import Foundation
    
    print(floor(3.9)) // Output: 3.0
    print(floor(5.2)) // Output: 5.0
    print(floor(7.0)) // Output: 7.0
    
  • We can use this function if we want to compute the integral part of some division (quotient):

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

Integer division

Swift has no separate floor-division operator. When both operands are Int, the normal division operator (/) returns only 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:

let a = 3
let b = 7

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

Modulo

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

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

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

print(b % a)  // Output: 0
print(11 % b) // 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:
let quotient = 26 / 3
let remainder = 26 % 3

print(quotient)  // Output: 8
print(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

    SWIFT · CALCULATIONS WITH NUMBERS

    Floor Division in Swift, explained

    Quotient and remainder

    When you divide one whole number by another, the quotient is how many times the divisor fits completely, and the remainder is what is left over. For 15 divided by 4, the quotient is 3 and the remainder is 3, because 4 * 3 + 3 = 15.

    Division of Int values

    Swift has no separate floor-division operator. The ordinary / does the job when both operands are Int: it returns the quotient and drops the fractional part.

    print(10 / 2)   // 5
    print(15 / 4)   // 3
    print(20 / 3)   // 6
    
    let a = 3
    let b = 7
    print(20 / a)   // 6
    print(b / a)    // 2
    

    The remainder comes from %:

    let quotient = 26 / 3
    let remainder = 26 % 3
    
    print(quotient)    // 8
    print(remainder)   // 2
    

    If you need both values at once, 26.quotientAndRemainder(dividingBy: 3) returns the pair (quotient: 8, remainder: 2).

    Truncation versus floor

    Integer division in Swift truncates: it rounds toward zero. For positive numbers that is the same as rounding down, and the two ideas only separate when a result is negative:

    let neg = -7
    let two = 2
    print(neg / two)   // -3
    print(neg % two)   // -1
    

    The exact value of -7 / 2 is -3.5. Rounding down would give -4, but Swift gives -3, and the remainder takes the sign of the dividend, so that (neg / two) * two + neg % two still equals -7. Programs that need true floor behaviour with negative numbers have to compute it themselves.

    The floor() function

    The function floor takes a decimal number and returns the largest whole number that is less than or equal to it. It works on floating-point values and returns a Double. It belongs to Foundation, so the file needs an import:

    import Foundation
    
    print(floor(3.9))    // 3.0
    print(floor(5.2))    // 5.0
    print(floor(7.0))    // 7.0
    print(floor(-3.9))   // -4.0
    print(Int(floor(3.9)))   // 3
    

    Without import Foundation, the call fails with cannot find 'floor' in scope. The result is a Double, which is why it prints as 3.0; wrap it in Int(...) for a whole number. Floor of a negative decimal goes down, to -4.0, which is where it differs from integer division.

    For this lesson you don't need floor: dividing two Int values already gives the quotient.

    Getting a floored quotient yourself

    If you do need the floor of a division that might be negative, convert to Double, divide, floor and convert back: Int(floor(Double(neg) / Double(two))) is -4 for -7 and 2. For non-negative values, plain / is all you need.

    The exercise

    A product costs price dollars and Andy has amount dollars. He can't buy a fraction of a product, so the program must print how many whole products he can afford. The starter code is:

    let amount: Int = 70
    let price: Int = 15
    

    The number of whole products is the quotient of amount divided by price. Both constants are Int, so the division drops the fraction: 70 divided by 15 is 4.67, and the result is 4.

    The solution in Swift

    let amount: Int = 70
    let price: Int = 15
    
    print(amount / price)
    

    The output is:

    4
    

    Common mistakes

    • Converting to Double first. Double(amount) / Double(price) gives 4.666666666666667, which keeps the fraction the task wants removed.
    • Assigning an Int division to a Double. let q: Double = a / b with two Int values doesn't compile: cannot convert value of type 'Int' to specified type 'Double'.
    • Dividing by zero. 7 / 0 on Int values stops the program with a runtime error. Double division by zero gives inf instead.
    • Using % on decimals. 7.5 % 2 doesn't compile in Swift. The message points to truncatingRemainder(dividingBy:).

    Key takeaways

    • With Int operands, / returns the quotient and % returns the remainder.
    • Integer division rounds toward zero; floor rounds down and needs import Foundation.
    • Dividing amount by price counts how many whole items the money buys.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free