Python 3 – Number floor() Method
The floor method is a Python 3 method that is distinguished by its ability to return the next whole number that is equal to or lower than a particular value that has been passed into it. The function is unique and different from the standard round() function that rounds the values up and down to the nearest possible value. This article will explore the different applications of the floor() method and how coding in Python 3 can be used to achieve this.
Syntax
The syntax for the floor() method is as shown below:
import math
math.floor(x)
Here, the math.floor() method takes in only one argument called “x” and returns the next integer value that is equal to or less than the given argument. The import statement is necessary to bring in the math module and access the floor() function.
Parameters
The floor() method only takes in one parameter “x,” which is an argument that can be any numeric data type, either a positive or negative integer, float, or string representing a numeric value. If the given value is a string, the floor() method will first convert the string into a numeric data type (either an integer or a float) and round it down to the nearest lower integer.
The following code example shows how the floor() method can be used with different data types:
import math
# Uses float data type
x = 19.8
print(math.floor(x)) # Outputs 19
# Uses integer data type
y = 75
print(math.floor(y)) # Outputs 75, as integer is already rounded down
# Uses a string representation of a number
z = "18.5"
print(math.floor(float(z))) # Outputs 18, converts the string to a float before rounding it down
Return Value
The floor() method in Python 3 returns the next integer value that is equal to or less than the argument passed to it. The return value is always an integer, which means that any decimal places are truncated.
import math
x = 16.5
print(math.floor(x)) # Returns 16, instead of 16.5
Examples
Example 1: Using the floor method with Positive and Negative Decimal Values
The first example shows how to use the floor() method to return the next available lower integer of different decimal values. The code snippet below takes in different decimal numbers and uses the floor() method to round them down to the nearest lower integer.
import math
num1 = 12.50
num2 = -29.78
num3 = 7.89
num4 = -3.29
print("Floor value of", num1, "is", math.floor(num1)) # Output: 12
print("Floor value of", num2, "is", math.floor(num2)) # Output: -30
print("Floor value of", num3, "is", math.floor(num3)) # Output: 7
print("Floor value of", num4, "is", math.floor(num4)) # Output: -4
Example 2: Using The floor() Method to Implement The Countdown Timer
In this example, we will explore how we can use the floor() method to set up a countdown timer that begins from the number 60 and decreases by one every second to the number 0, without including the numbers in between.
import time
import math
countdown = 60
while countdown>=0:
print("Countdown:",math.floor(countdown))
time.sleep(1)
countdown-=1
After running the code above, it will output the following text:
Countdown: 60
Countdown: 59
Countdown: 58
Countdown: 57
Countdown: 56
Countdown: 55
Countdown: 54
Countdown: 53
Countdown: 52
Countdown: 51
Countdown: 50
Countdown: 49
Countdown: 48
Countdown: 47
Countdown: 46
Countdown: 45
Countdown: 44
Countdown: 43
Countdown: 42
Countdown: 41
Countdown: 40
Countdown: 39
Countdown: 38
Countdown: 37
Countdown: 36
Countdown: 35
Countdown: 34
Countdown: 33
Countdown: 32
Countdown: 31
Countdown: 30
Countdown: 29
Countdown: 28
Countdown: 27
Countdown: 26
Countdown: 25
Countdown: 24
Countdown: 23
Countdown: 22
Countdown: 21
Countdown: 20
Countdown: 19
Countdown: 18
Countdown: 17
Countdown: 16
Countdown: 15
Countdown: 14
Countdown: 13
Countdown: 12
Countdown: 11
Countdown: 10
Countdown: 9
Countdown: 8
Countdown: 7
Countdown: 6
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Countdown: 0
The code above demonstrates how to use the floor() method to set up the clock countdown timer.
Conclusion
The floor() method is a unique and straightforward Python 3 function that rounds down any numerical value passed into it to the nearest equal or lower integer. This article has explored how the floor() method works and given examples of when to apply it in different scenarios. By incorporating the above code examples into your Python 3 code, you will be able to harness the full power of the floor() method in your applications.