EN
Python - math.floor() method example
0 points
The math.floor()
function returns a value that is smaller than or equal to the argument - the result of round down operation.
xxxxxxxxxx
1
import math
2
3
print(math.floor(5.0)) # 5
4
5
print(math.floor(2.49)) # 2
6
print(math.floor(2.50)) # 2
7
print(math.floor(2.51)) # 2
8
9
print(math.floor(-2.49)) # -3
10
print(math.floor(-2.50)) # -3
11
print(math.floor(-2.51)) # -3
12
13
print(math.floor(0.999)) # 0
14
print(math.floor(1.001)) # 1
15
print(math.floor(-1.001)) # -2
Syntax |
xxxxxxxxxx 1 math.floor(number) |
Parameters | number - number that specifies the number to round down. |
Result |
Rounded down |
Description | floor is a method that takes only one parameter and returns a rounded down value. |
xxxxxxxxxx
1
import math
2
3
4
def floor_precised(number, precision):
5
power = math.pow(10, precision)
6
return math.floor(number * power) / power
7
8
9
print(floor_precised(5, 0)) # 5.0
10
print(floor_precised(5.0, 0)) # 5.0
11
print(floor_precised(.5, 0)) # 0.0
12
13
print(floor_precised(1.1234, 0)) # 1.0
14
print(floor_precised(1.1234, 1)) # 1.1
15
print(floor_precised(1.1235, 2)) # 1.12
16
print(floor_precised(1.1235, 3)) # 1.123
17
18
print(floor_precised(-1.1234, 0)) # -2.0
19
print(floor_precised(-1.1234, 1)) # -1.2
20
print(floor_precised(-1.1234, 2)) # -1.13
21
print(floor_precised(-1.1234, 3)) # -1.124
22
23
print(floor_precised(1234, -1)) # 1230.0
24
print(floor_precised(1234, -2)) # 1200.0
25
print(floor_precised(1234, -3)) # 1000.0
26
27
print(floor_precised(5_000.000_001, 0)) # 5000.0
28
print(floor_precised(5_000.000_001, 6)) # 5000.000001
29
print(floor_precised(5_000.000_001, -3)) # 5000.0