Do you know it is easier to remember 10 than 9.8 or 10.2? Why? Because whole numbers are easier to work with than decimals.
Rounding off is the most basic yet essential part of programming.
Rounding is the practice of simplifying a number without modifying much of its value. For instance, 9.8 rounded to 10, with only a 0.2 difference. Likewise, 10.2 rounded to 10, with the same difference. So the only goal is to get a value that is close to the original value but in a simpler form. Let's dive deeper into rounding down in Python!
What is round down?
So far, we've discussed "Round off," which is the most general type of approximation. "Round down," while similar, is not the same. As the name implies, Round Down reduce a number to the nearest lower integer.
For example -
Value after Round Down
The above table is an overview of how rounding down values works.
You might be wondering, how is this different from Round up?
It's quite simple. Round Up, as the name implies, rounds the value to the nearest higher integer, whereas Round Down rounds the value to the nearest lower integer.
Look at the diagram below. You can also think of it as Round up going to the right of the number line while Round down moves towards the left.
Now that we know what Round Down is, let's look at how to round down values in programming.
How to Round Down a Number in Python?
There are various methods to round down a number in python. Let's take a look at them.
Method 1: Using string format
This method is applicable when you only want to show the whole number before the decimal point without altering the value. This method only displays the whole number and does not round down the value.
Check out the code below:
Method 2: Using int() method
The int() method terminates the fractional values by returning a whole number. This method converts the given number's data type to an integer.
It is worth noting that when dealing with negative numbers, it returns the number away from zero.
Method 3: Using in-built round() method
Python has an in-built round() method to round off any number.
It takes two parameters as input -
- num - The number to be rounded.
- decimal_places - The number of digits to be rounded off (default being 0).
Let's try rounding off a number for different decimal places -
(Attention!!) Although the round() method has made rounding off easier, it doesn't always "round down" the number (Remember the difference between round up and round down).
Is it hard to figure out? No worries, let's walk through this with the help of a table-
|Number||Decimal places to be rounded off to||Output|
It's pretty simple to observe now, isn’t it? Data tabularization is always effective ;)
Method 4: Using trunc() method
The truncate method, also known as trunc(), is a built-in method of the math module. This method returns the integer part of a given decimal number. trunc(), as the name implies, shortens the number rather than rounding it up. Sometimes truncating the number is a better solution for "Round Down in Python".
Confused with the direct import? You can always try the following code:-
(Ps: Both the code practically indicate the same thing and has the same output)
Method 5: Using math.floor() method
The floor() method is also part of the math module. This method returns the nearest integer that is less than or equal to a given number.
See the syntax below:
You can always try the below alternate code:
Both the codes above will yield the same output.
From the above code, you can note how the floor() method works with negative numbers. It rounds down the negative number away from zero (Here, -0.6 to 1).
Method 6: Using // operator
Python's floor division operator, aka the integer division operator, is like math.floor() method. It divides the first number by the second and then rounds down the result to the nearest lower integer. You can always compare its similarity with the floor() method.
Method 7: Using decimal module (.quantize() method)
Python includes a decimal module for dealing with decimal numbers. It enables easy representation and precision. Using the .quantize() method, we can round off a number. But before using the methods, an instance of the Decimal class must be created.
Too large to handle? Don't worry, the code below will help you understand-
The above lines of code display the properties of a decimal instance.
You'll notice that by default, rounding = 'ROUND_HALF_EVEN". Now to begin with the round-down process -
Look how this lengthy piece of code produced a one-line output! That's why the use of decimal module is preferred when dealing with actual decimal or float numbers, else it only adds up to the lines of code.
Other Python modules, such as NumPy and Pandas, provide round down functionality. But, as with the decimal module, these are also preferred when working with large data sets.
Round Down Array Elements in Python
Any of the above-listed methods can be used to round down a number. There are two ways to apply these methods in an array -
- Using for loop
- Using list comprehension
Method 1: Using forloop
Using a for loop is the easiest way to round down all the array elements. You can easily iterate through each array element, applying the round down method individually.
See through the below code for a better understanding -
Here, math.floor() method is applied to each element of the array, and is stored back at the same index.
Method 2: Using list comprehension
Compressing the for loop reduces the lines of code, along with increasing the readability of the code. The array can be rounded down using list comprehension as-
Note that the obtained result is the same, irrespective of the way used.
In this article, we have covered various methods to round down in python. When working with data and numbers, you are bound to come across round down, whether working with float numbers or obtaining whole numbers as output. We have understood several methods to round down in python along with their needs. We hope you select the method as per the requirement.