How to Divide without a remainder in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
2 min

banner

# Divide without a remainder in Python

Use the floor division operator // to divide without a remainder.

The floor division operator will always return an integer and is like using mathematical division with the floor() function applied to the result.

main.py
result_1 = 30 // 6 print(result_1) # ๐Ÿ‘‰๏ธ 5 print(type(result_1)) # ๐Ÿ‘‰๏ธ <class 'int'> result_2 = 30 / 6 print(result_2) # ๐Ÿ‘‰๏ธ 5.0 print(type(result_2)) # ๐Ÿ‘‰๏ธ <class 'float'>

divide without remainder

The code for this article is available on GitHub

We used the floor division // operator to return an integer from integer division.

Division / of integers yields a float, while floor division // of integers result in an integer.

The result of using the floor division operator is that of a mathematical division with the floor() function applied to the result.

main.py
print(11 // 2) # ๐Ÿ‘‰๏ธ 5 print(11 / 2) # ๐Ÿ‘‰๏ธ 5.5

# Using math.floor()

You can use the math.floor() method in a similar way to how we used the floor division // operator.

main.py
import math result_1 = math.floor(11 / 2) print(result_1) # ๐Ÿ‘‰๏ธ 5 result_2 = 11 / 2 print(result_2) # ๐Ÿ‘‰๏ธ 5.5

using math floor method

The code for this article is available on GitHub

The math.floor() method returns the largest integer less than or equal to the provided number.

# Using math.ceil()

There is also a math.ceil() method.

main.py
import math result_1 = math.ceil(11 / 2) print(result_1) # ๐Ÿ‘‰๏ธ 6 result_2 = 11 / 2 print(result_2) # ๐Ÿ‘‰๏ธ 5.5

using math ceil method

The code for this article is available on GitHub

The math.ceil() method returns the smallest integer greater than or equal to the provided number.

# Using round()

You can also use the round() function if you want to round to the nearest integer when dividing.

main.py
result_1 = round(15 / 4) print(result_1) # ๐Ÿ‘‰๏ธ 4 result_2 = 15 / 4 print(result_2) # ๐Ÿ‘‰๏ธ 3.75
The code for this article is available on GitHub

The round() function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal, the number should have after the operation (optional)

The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

# Divide without a remainder using int()

You can also use the int() class to remove the division decimal.

The int() class truncates floating-point numbers toward zero, so it will return an int that represents the number without the decimal places.

main.py
result_1 = int(7 / 2) print(result_1) # ๐Ÿ‘‰๏ธ 3 result_2 = int(-7 / 2) print(result_2) # ๐Ÿ‘‰๏ธ -3

We used the int() class to remove the division decimal.

The int class returns an integer object constructed from the provided number or string argument.

If passed a floating-point number, the int() class truncates toward zero.

This approach allows us to remove the division decimal from a negative number and get a predictable result.

main.py
result_2 = int(-7 / 2) # ๐Ÿ‘‰๏ธ = -3.5 print(result_2) # ๐Ÿ‘‰๏ธ -3

# Divide without a remainder using math.trunc()

Alternatively, you can use the math.trunc() method.

The math.trunc() method removes the fractional part and returns the integer part of the given number.

main.py
import math result_1 = math.trunc(7 / 2) print(result_1) # ๐Ÿ‘‰๏ธ 3 result_2 = math.trunc(-7 / 2) print(result_2) # ๐Ÿ‘‰๏ธ -3
The code for this article is available on GitHub

The math.trunc() method takes a number, removes its fractional part and returns its integer part.

The math.trunc() method rounds towards zero.

main.py
print(math.trunc(3.45)) # ๐Ÿ‘‰๏ธ 3 print(math.trunc(-3.45)) # ๐Ÿ‘‰๏ธ -3

This approach achieves the same result as passing the result from the division to the int() class.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev