In today’s post, we’ll look at how we can round a floating-point number down to the nearest integer in Python. Specifically, we’ll be looking at the Python math.floor()
function and the differences between the math.floor()
function and other similar functions.
For our practice question, we’ll be writing a function that rounds a number down to a specific decimal place.
Here are some concepts we’ll cover in today’s post:
- How to use the built-in
math.floor()
function - What is the difference between the
floor()
,ceil()
,round()
andint()
functions - How to round a number down to a specific decimal place in Python
Table of Contents
Key Concepts
The math.floor() function
The math.floor()
function is used to round a number down to an integer in Python. This function accepts a floating-point number n
and returns the nearest integer smaller than n
.
Let’s look at some examples:
import math
print(math.floor(1.01))
print(math.floor(6.99))
The math.floor()
function is in the math
module. Hence, we need to import it first on line 1.
Next, we call the function twice to round the numbers 1.01 and 6.99 down to the nearest integer.
If you run the code above, you’ll get
1
6
as the output.
As 1.01 is between the integers 1 and 2, the floor()
function returns the smaller number (1) as the result.
Similarly, as 6.99 is between 6 and 7, the function returns 6 (i.e. the smaller number).
Let’s see what happens when we pass negative numbers to the function:
print(math.floor(-1.01))
print(math.floor(-6.99))
In the examples above, we pass -1.01 and -6.99 to the function. If you run the examples above, you’ll get
-2
-7
as the output. As -1.01 is between -1 and -2, the function returns the smaller number, which is -2. Similarly, as -6.99 is between -6 and -7, the function returns -7.
The math.ceil() function
In the previous section, we looked at the math.floor()
function. This function rounds a floating-point number down to the nearest integer.
If you want to round a floating-point number up to the nearest integer, you can use the ceil()
function instead.
Let’s look at some examples:
import math
print(math.ceil(1.01))
print(math.ceil(6.99))
print(math.ceil(-1.01))
print(math.ceil(-6.99))
As the ceil()
function is also in the math
module, we need to import the module first on line 1. Next, we call the function four times, passing the values 1.01, 6.99, -1.01 and -6.99 to the function.
If you run the code above, you’ll get the following output:
2
7
-1
-6
In each case, the function gives us the nearest integer that is greater than the argument we pass in. For instance, when the argument is -6.99 (the last example), we get -6 as the output. This is because -6.99 is between -6 and -7. Since -6 is the larger of the two, the ceil()
function returns -6 as the answer.
The round() function
Next, let’s talk about the round()
function. Similar to the floor()
and ceil()
functions, this function is for rounding a floating-point number.
However, there are three major differences between the round()
function and the ceil()
and floor()
functions.
Firstly, the round()
function is not in the math
module. Hence, we do not need to import any module to access the function.
Next, the round()
function allows us to specify the number of decimal places to round a number off to. It accepts two arguments – the number to be rounded off and the desired number of decimal places. The desired number of decimal places is optional. If it is not specified, the default is 0.
Let’s look at some examples:
print(round(1.234567))
print(round(1.234567, 2))
If you run the code above, you’ll get the following output:
1
1.23
In the first example, as we did not pass a second argument to the round()
function, it rounds the number 1.234567 off to the nearest integer.
In the second example, we pass the number 2 to the function as the second argument. Hence, the function rounds the number off to 2 decimal places and we get 1.23 as the output.
Last but not least, the round()
function differs from the floor()
and ceil()
functions in how it rounds a number. The round()
function does not round up or down by default. Instead, it rounds a number up if the digit after the specified decimal place is greater than or equal to 5. Else, it rounds the number down.
For instance, round(9.2184, 3)
gives us 9.218 as the answer because the number after the third decimal place is 4 (which is smaller than 5). In contrast, round(6.275, 2)
gives us 6.28 as the number after the second decimal place is 5.
The int() function
Finally, let’s talk about the int()
function. This function is another built-in function in Python and is used to convert a string or number to an integer. In some cases, it achieves the same result as the floor()
function. However, there are two key differences between the int()
and floor()
functions.
Firstly, we do not need to import the math
module to use the int()
function.
Secondly, the int()
function does not round a floating-point number down to an integer. Instead, it truncates the decimal portion of the number.
Let’s look at some examples:
print(int(1.01))
print(int(6.99))
print(int(-1.01))
print(int(-6.99))
Here, we use the int()
function to convert the numbers 1.01, 6.99, -1.01 and -6.99 to integers. These are the same numbers we used previously to demonstrate the math.floor()
function. If you run the code above, you’ll get the following output:
1
6
-1
-6
If you compare the output above with the output we got with the math.floor()
function, you’ll probably notice that the int()
function gives the same output as the floor()
function when the number is positive.
In contrast, when the number is negative, the int()
function does not give the same result as the math.floor()
function. Instead, it gives us an integer that is greater than the number we pass to the function. This is because the int()
function simply truncates the decimal portion of the number.
-6.99 without the decimal portion becomes -6, which is greater than -6.99. Hence, when the number is negative, the int()
function works like the ceil()
function.
Practice Question
Today’s practice question requires us to write a function called myFloor()
that rounds a floating-point number down to a specified number of decimal places (instead of an integer).
It accepts two arguments – the number to round down and the number of decimal places to round it down to. The second argument should have a default value of 0.
For instance, myFloor(1.2365, 2)
rounds the number 1.2365 down to 2 decimal places. We should get 1.23 as the result.
On the other hand, myFloor(1.2365)
rounds 1.2365 down to 0 decimal places (i.e. to an integer). We should get 1 as the result.
Last but not least, myFloor(-2.3467, 2)
rounds -2.3467 down to 2 decimal places. We should get -2.35 as the result. This may be a bit confusing for some readers. The reason we get -2.35 is because -2.3467 is between -2.34 and -2.35. Since myFloor()
rounds a number down, the correct answer is the smaller number, which is -2.35.
Expected Results
You can run the statements below to test your function:
print(myFloor(1.23456, 3))
print(myFloor(3.129981, 2))
print(myFloor(-6.7274, 2))
print(myFloor(9.879))
print(myFloor(-10.434))
You should get the following output:
1.234
3.12
-6.73
9
-11
Additional Notes
Hint:
Rounding a floating-point number like 1.2345 down to 2 decimal places is the same as rounding 123.45 (i.e. 1.2345×100) down to an integer and dividing the answer by 100.
Suggested Solutions
Solution 1
import math
def myFloor(number, dp = 0):
number = number * 10**dp
number = math.floor(number)
number = number / 10**dp
if dp == 0:
return int(number)
else:
return number
The solution above uses the built-in floor()
function in Python. Hence, we need to import the math
module on line 1.
Next, we define a function called myFloor()
that has two parameters – number
and dp
(which has a default value of 0).
Inside the function, we first multiply number
by 10 to the power of dp
(i.e. 10**dp
) and assign the result back to number
(line 4). Next, we use the floor()
function to round number
down to the nearest integer and assign the result back to number
(line 5). Finally, we convert number
to the desired number of decimal places and once again assign the result back to number
(line 6).
Let’s use an example to illustrate how this works.
Suppose number = 1.2345
and dp = 3
, we multiply number
by 103 to get the number 1234.5 on line 4.
Next, we use the floor()
function to round 1234.5 down to the nearest integer. This gives us 1234 as the answer.
Finally, we convert 1234 back to a floating-point number with three decimal places by dividing it by 103 (on line 6). This gives us the value 1.234, which is the answer that we want.
Once we have the answer that we want, we are ready to return the answer. However, before we do that, we first check if dp
equals 0 (on line 8). If it equals, we know that the answer should be an integer. Due to the fact that Python 3 always gives a floating-point number when we do division, the answer we got on line 6 is definitely a floating-point number (such as 2.0). Hence, we use the int()
function to convert it to an integer first before returning it (on line 9).
On the other hand, if dp
is not equal to 0, we simply return the result we got (on line 11).
With that, the function is complete.
Next, let’s look at a solution that does not use the built-in floor()
function.
Solution 2
def myFloor(number, dp = 0):
number = number * 10**dp
if number > 0:
number = int(number)
else:
number = int(number)-1
number = number / 10**dp
if dp == 0:
return int(number)
else:
return number
The solution above is very similar to Solution 1. The only difference is that it doesn’t use the built-in floor()
function. Instead, it uses an if
-statement and the int()
function. If number
is positive (line 4), the int()
function gives the same result as the floor()
function. Hence, we simply assign the result of the int()
function to number
.
On the other hand, if number
is not positive, we know that int()
gives the nearest integer that is larger than number
. Hence, we subtract the integer by 1 to get the nearest integer that is smaller than number
.
For instance, if number = -4.6
, int(number)
gives us -4 while math.floor(number)
gives us -5.
Hence, int(number) - 1
gives us the same result as math.floor(number)
.
Other than that, Solution 2 is the same as Solution 1.
That’s all for today’s practice.