Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to round down in Python

Introduction

In mathematics and computer programming, rounding is the process of adjusting a fractional number to make it as close as possible to a whole number. Rounding down, also known as "flooring" a number, is the process of rounding a number down to the nearest whole number. In this blog post, we will explore how to round down numbers in Python. We will look at different methods and functions, discuss their advantages and disadvantages, and provide some code examples to help you understand the concept better.

Whether you are a student learning programming or a professional software developer, understanding how to round down numbers is crucial in many applications. For example, rounding down can be used in financial calculations, creating evenly spaced intervals in data analysis, or even determining the number of items that can fit in a container.

Understanding Rounding and Rounding Down

Before diving into the code, let's first clarify the difference between rounding and rounding down. When we round a number, we either round it up or down depending on its fractional part. If the fractional part is equal to or greater than 0.5, we round up; otherwise, we round down.

Rounding down, on the other hand, always rounds the number down to the nearest whole number, regardless of its fractional part. For example:

  • Rounding 4.3 gives us 4 (because the fractional part is less than 0.5)
  • Rounding down 4.3 gives us 4 (ignoring the fractional part)

Now that we have a clear understanding of the concept, let's dive into the different ways to round down numbers in Python.

Using the math.floor() Function

Python provides a built-in library called math that contains various mathematical functions. One of these functions is floor(), which takes a number as its input and returns the largest integer less than or equal to the input number. This is effectively rounding down the number.

To use the math.floor() function, you first need to import the math library. Here's an example of how you can round down a number using math.floor():

import math

number = 4.7
rounded_down = math.floor(number)
print(rounded_down)  # Output: 4

In this example, we imported the math library and used the floor() function to round down the number (4.7) to the nearest whole number (4). The floor() function works with both positive and negative numbers:

import math

positive_number = 4.7
negative_number = -4.7

rounded_down_positive = math.floor(positive_number)
rounded_down_negative = math.floor(negative_number)

print(rounded_down_positive)  # Output: 4
print(rounded_down_negative)  # Output: -5

As you can see, the floor() function rounds down -4.7 to -5, not -4. This is because -5 is the largest integer less than or equal to -4.7.

Using the int() Function

Another method for rounding down numbers in Python is by using the built-in int() function. The int() function takes a number as its input and returns the integer portion of the number, effectively rounding down the number. Here's an example of how you can round down a number using the int() function:

number = 4.7
rounded_down = int(number)
print(rounded_down)  # Output: 4

In this example, the int() function rounded down the number (4.7) to the nearest whole number (4). However, the int() function behaves differently for negative numbers than the math.floor() function:

positive_number = 4.7
negative_number = -4.7

rounded_down_positive = int(positive_number)
rounded_down_negative = int(negative_number)

print(rounded_down_positive)  # Output: 4
print(rounded_down_negative)  # Output: -4

As you can see, the int() function rounds down -4.7 to -4, not -5. This is because the int() function simply truncates the decimal part of the number, effectively rounding it towards zero.

This difference in behavior for negative numbers is important to keep in mind when choosing between the math.floor() and int() functions for rounding down numbers in your code.

Using the divmod() Function

The divmod() function is another built-in Python function that can be used to round down numbers. The divmod() function takes two numbers as input (a numerator and a denominator) and returns a tuple containing the quotient and the remainder. The quotient is the result of the division rounded down to the nearest integer.

Here's an example of how you can round down a number using the divmod() function:

number = 4.7
rounded_down, remainder = divmod(number, 1)
print(rounded_down)  # Output: 4

In this example, we used the divmod() function to divide the number (4.7) by 1. The result is a tuple containing the rounded-down quotient (4) and the remainder (0.7). We then used tuple unpacking to assign the rounded-down quotient to the rounded_down variable.

Similar to the int() function, the divmod() function behaves differently for negative numbers than the math.floor() function:

positive_number = 4.7
negative_number = -4.7

rounded_down_positive, _ = divmod(positive_number, 1)
rounded_down_negative, _ = divmod(negative_number, 1)

print(rounded_down_positive)  # Output: 4
print(rounded_down_negative)  # Output: -4

As you can see, the divmod() function rounds down -4.7 to -4, not -5. This is because the divmod() function returns the quotient rounded towards zero.

Conclusion

In this blog post, we explored different ways to round down numbers in Python. We discussed the math.floor() function, the int() function, and the divmod() function, and provided code examples to help you understand how to use these functions in your code. We also highlighted the differences in behavior between these functions, especially when dealing with negative numbers.

When choosing a method for rounding down numbers in Python, it is essential to consider the specific requirements of your application and the behavior of the function for both positive and negative numbers. If you need consistent rounding down behavior for both positive and negative numbers, the math.floor() function is the best choice. However, if you only need to round down positive numbers or prefer rounding towards zero for negative numbers, the int() or divmod() functions may be more suitable.