Start learning free

LESSON 22 OF 338 · CALCULATIONS WITH NUMBERS

Floor Division in Go: Integer Division and Truncation

Lesson Read the full explanation

TL ; DR:

  • When both operands are integers, Go division returns the integral part of the result (the quotient):

    fmt.Println(10 / 2) // Output: 5
    fmt.Println(15 / 4) // Output: 3
    fmt.Println(20 / 3) // Output: 6
    

  • It can also be used with variables:

    a := 3
    b := 7
    
    fmt.Println(20 / a) // Output: 6
    fmt.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

Integer division

Go does not have a separate // operator. If both values are integers, / returns the quotient:

fmt.Println(10 / 2) // Output: 5
fmt.Println(15 / 4) // Output: 3
fmt.Println(20 / 3) // Output: 6

It can also be used with variables:

a := 3
b := 7

fmt.Println(20 / a) // Output: 6
fmt.Println(b / a)  // Output: 2

Modulo

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

fmt.Println(10 % 2) // Output: 0
fmt.Println(15 % 4) // Output: 3
fmt.Println(20 % 3) // Output: 2

a := 2
b := 4

fmt.Println(b % a)  // Output: 0
fmt.Println(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:
quotient := 26 / 3
remainder := 26 % 3

fmt.Println(quotient)  // Output: 8
fmt.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

    GO · CALCULATIONS WITH NUMBERS

    Floor Division in Go, explained

    What integer division is

    Division between whole numbers does not always come out even. Dividing 15 by 4 gives a quotient of 3 (how many times 4 fits completely) and a remainder of 3 (what is left over). Programs often need only the whole part, for example to count how many full groups fit into a total.

    Go has no separate operator for this. There is no //. The ordinary / operator does the job: when both operands are integers, the result is an integer, and the fractional part is thrown away.

    fmt.Println(10 / 2)  // 5
    fmt.Println(15 / 4)  // 3
    fmt.Println(20 / 3)  // 6
    

    The same works with variables:

    a := 3
    b := 7
    
    fmt.Println(20 / a)  // 6
    fmt.Println(b / a)   // 2
    

    Quotient and remainder together

    The operator % gives the remainder, so the pair / and % describes a division completely:

    quotient := 26 / 3
    remainder := 26 % 3
    
    fmt.Println(quotient)   // 8
    fmt.Println(remainder)  // 2
    

    The two results always fit together: quotient * 3 + remainder gives back the original 26. Here 8 * 3 + 2 is 26.

    Truncation, not flooring

    For positive numbers, dropping the fraction is the same as rounding down, which is why this lesson is called floor division. For negative numbers, the two differ. Go truncates toward zero:

    fmt.Println(-7 / 2)  // -3
    fmt.Println(-7 % 2)  // -1
    fmt.Println(7 / -2)  // -3
    fmt.Println(7 % -2)  // 1
    

    Rounding -3.5 down would give -4, but Go gives -3. The remainder follows the sign of the left operand (the dividend), and the rule quotient * divisor + remainder == dividend still holds: (-3) * 2 + (-1) is -7. If your program needs a true floor for negatives, you have to adjust the result yourself.

    Integers versus decimals

    The behaviour depends on the types, not on the operator. If either operand is a float64, the division is exact:

    x := 5
    y := 2
    
    fmt.Println(x / y)                    // 2
    fmt.Println(float64(x) / float64(y))  // 2.5
    fmt.Println(float64(x / y))           // 2
    

    The last line shows a common trap: the division of two integers happens first and loses the 0.5, and only then is the result converted to a decimal. To get 2.5, convert the operands before dividing. Go never mixes int and float64 on its own, so the conversion is always explicit.

    The lesson's exercise

    A product costs price dollars and Andy has amount dollars. He can't buy a fraction of a product, so the question is how many whole products he can afford. That is the quotient of amount divided by price.

    var amount int = 70
    var price int = 15
    
    fmt.Println(amount / price)
    

    The program prints 4. The exact value of 70 / 15 is about 4.67, and truncation leaves the 4 whole products. Four products cost 60 dollars, so 10 are left, which is exactly the remainder 70 % 15.

    Quotient and remainder answer two different questions about the same division: how many whole products Andy can buy, and how much money he is left with. Real programs often compute both, and the same pair appears whenever you split a total into full groups and a leftover, such as seconds into minutes and seconds.

    Common mistakes and gotchas

    • Expecting decimals from two integers. 7 / 2 is 3. If you want 3.5, at least one operand must be a float64.
    • Converting too late. float64(x / y) divides first. Write float64(x) / float64(y).
    • Assuming rounding to the nearest. 7 / 4 is 1, not 2, although 1.75 is closer to 2.
    • Negative operands. The result is truncated toward zero, so -7 / 2 is -3 and not -4.
    • Dividing by zero. With an integer variable as divisor, the program stops at run time with panic: runtime error: integer divide by zero. A literal zero divisor, as in 5 / 0, is rejected by the compiler instead.
    • Using //. That starts a comment in Go, so everything after it on the line is ignored.

    Key takeaways

    • With two integers, / returns the quotient and drops the fraction.
    • % returns the remainder, and (a / b) * b + a % b == a.
    • Go truncates toward zero, which equals rounding down only for non-negative results.
    • Decimal division needs a float64 operand, converted before the division.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free