Python中的math.floor

Python的math floor函数是math库中可用的数学函数之一。此floor函数用于返回小于或等于指定表达式或值的最接近的整数值。

在本文中,我们将向您展示如何在编程中使用math floor函数,并附带示例以及math库中的语法。

math.floor (Expression)

Python math Floor函数示例

floor函数返回小于或等于给定数值的最接近的整数值。让我向您展示一个返回12.95最接近值的floor函数示例。

import math
 
val = math.floor(12.95)
print(val)
12

在此floor示例中,它接受参数并返回正值和负值的最小整数值。

import math
 
print('The final result of math.floor(10.45) = ', math.floor(10.45))
print('The final result of math.floor(20.99) = ', math.floor(20.99))
print()

print('The final result of math.floor(-120.49) = ', math.floor(-120.49))
print('The final result of math.floor(-160.99) = ', math.floor(-160.99))
math floor Function Example

Python math.floor函数示例

下面的查询将向您展示使用此math floor函数的多种方法。

import math # This allows us to use the floor function

a = 0.24
b = -90.98
c = 45.05
d = 45.98
e = math.floor(2.45 + 7.55 - 14.88)
pi = math.floor(math.pi)

x = [-22.45, 2.40, 9.65] # List Example
y = (-2.45, 22.10, 22.95) # Tuple Example

print(" The Value of 'a' after the floor function is: ", math.floor(a))
print(" The Value of %.2f after the floor function is: %d" %(b, math.floor(b)))
print(" The Value of %.2f after the floor function is: %d" %(c, math.floor(c)))
print(" The Value of %.2f after the floor function is: %d" %(d, math.floor(d)))
print(" The Value of 'e' after the floor function is: ", e)
print(" The Value of 'PI' after the floor function is: ", pi)
print("  ")

# printing List values 
print(" First Value from List is: ", math.floor(x[0]))
print(" Second Value from List is: ", math.floor(x[1]))
print(" Third Value from List is: ", math.floor(x[2]))
print("  ")

# printing Tuple values 
print(" First Value from List is: ", math.floor(y[0]))
print(" Second Value from List is: ", math.floor(y[1]))
print(" Third Value from List is: ", math.floor(y[2]))
 The Value of 'a' after the floor function is:  0
 The Value of -90.98 after the floor function is: -91
 The Value of 45.05 after the floor function is: 45
 The Value of 45.98 after the floor function is: 45
 The Value of 'e' after the floor function is:  -5
 The Value of 'PI' after the floor function is:  3
  
 First Value from List is:  -23
 Second Value from List is:  2
 Third Value from List is:  9
  
 First Value from List is:  -3
 Second Value from List is:  22
 Third Value from List is:  22

首先,我们使用以下语句导入了math库。这将使我们能够使用floor等数学函数。

import math

下面的Python代码行用于声明变量并分配值。

a = 0.24
b = -90.98
c = 45.05
d = 45.98

在下一行中,我们直接将math floor函数应用于多个值(一些计算)。

e = math.floor(2.45 + 7.55 - 14.88)

这意味着

floor (2.45 + 7.55 – 14.88)

floor (-4.88) = – 5

在下一行中,我们声明了列表元组。接下来,我们在print语句中使用floor函数来查找上述变量的最接近整数值。

int和floor之间的区别

这是一个经常被问到的问题,因为int和floor函数对于正值返回相同的结果。但是,如果您使用负值作为参数进行检查,您会发现区别。

import math
 
print('math.floor(100.45) Result = ', math.floor(100.45))
print('math.floor(200.99) Result = ', math.floor(200.99))
print('int(100.45) Result = ', int(100.45))
print('int(200.99) Result = ', int(200.99))
print()

print('math.floor(-150.49) result = ', math.floor(-150.49))
print('math.floor(-260.99) result = ', math.floor(-260.99))
print('int(-150.49) result = ', int(-150.49))
print('int(-260.99) result = ', int(-260.99))
math.floor(100.45) Result =  100
math.floor(200.99) Result =  200
int(100.45) Result =  100
int(200.99) Result =  200

math.floor(-150.49) result =  -151
math.floor(-260.99) result =  -261
int(-150.49) result =  -150
int(-260.99) result =  -260

floor除法示例

数学运算符返回除法的向下取整结果。

# floor Division example
 
a = 10
b = 3
 
x = a / b
print(x)
 
y = a // b
print(y)
3.3333333333333335
3

列表示例

让我对列表项使用此math floor函数。在这里,我们使用For循环迭代列表项,然后为每个项应用floor函数。

 
import math
 
numbers = [-10.89, 20.22, 40.67, -350.11, -450.91]
 
for num in numbers:
    print(math.floor(num))
-11
20
40
-351
-451

此Python math.floor函数示例与上面相同。但是,这次我们使用Map和Lambda函数来迭代项目。

import math
 
numbers = [-10.89, 20.22, 40.67, -350.11, -450.91]
print(numbers)
print()
 
floor_result = map(lambda num: math.floor(num), numbers)
print('The final result = ', list(floor_result))

floor函数与maplambda函数的输出

[-10.89, 20.22, 40.67, -350.11, -450.91]

The final result =  [-11, 20, 40, -351, -451]