Python Math.floor()方法

在Python中,math 模块提供了不同的数学函数供我们使用。math.floor() 方法是它的一个函数,它接受一个参数为浮点数,并返回小于所提供数字的最大整数。

如果指定的数字是一个整数,该方法返回与之相同的数字。方法math.floor() 是在Python 2.0中引入的,用来返回一个浮点数。

然而,在Python 3.0及以后的版本中,方法math.floor 总是返回整数数据类型。math.floor() 方法也是Python文档中理论和表示函数的一部分。

Pythonmath.floor() 方法的语法

math.floor(number)

参数

number 这是一个数字形式的必要参数。这个参数代表需要四舍五入的数字。

返回

该方法返回小于所提供的参数的最大或最多的整数,整数形式。在Python 3.0版本之前,它产生一个浮点数。

示例代码:使用math.floor() 方法

要使用方法floor() ,我们必须在 Python 中导入math 模块。使用该方法的正确方法是编写math.floor() ,并向其传递一个数字值。

如下面的例子所示,math.floor() 方法返回小于所提供数字参数的最大整数。

# imports the math module to use the floor() method
import math
# returns the largest integer less than the number
def floor_function(number):
    return math.floor(number)
# pass different types of data to the floor method
print("The floor method for number 77 is",floor_function(77))
print("The floor method for number 3.4 is",floor_function(3.4))
print("The floor method for number -4.2 is",floor_function(-4.2))
print("The floor method for number 11.0 is",floor_function(11.0))

输出:

# return integer values for different types of input values
The floor method for number 77 is 77
The floor method for number 3.4 is 3
The floor method for number -4.2 is -5
The floor method for number 11.0 is 11

示例代码:math.floor()math.ceil() 方法之间的区别

我们已经看到了floor() 方法是如何工作的。math.ceil() 方法也是接受一个必要的数字参数,并返回大于或等于所提供的数字参数的最小整数。

math 为了区分floor()ceil() 模块的方法,下面的例子说明了其结果。

import math
# function returns the greatest integer value but less than the provided number
def floor_function(number):
    return math.floor(number)
# function returns the smallest integer value but greater than the provided number
def ceil_function(number):
    return math.ceil(number)
# function test on positive and negative numbers each
print("The ceil method for number -1.56 is",ceil_function(-1.56))
print("The floor method for number -1.56 is",floor_function(-1.56))
print("The ceil method for number 23.22 is",ceil_function(23.22))
print("The floor method for number 23.22 is",floor_function(23.22))

输出:

# ceil returns the smallest integer less than the provided number
The ceil method for number -1.56 is -1
The floor method for number -1.56 is -2
The ceil method for number 23.22 is 24
# floor returns the largest integer greater than the provided number
The floor method for number 23.22 is 23

示例代码:int()floor() 方法之间的区别

正如我们在Python 3.0及以后的版本中知道的,floor() 方法返回整数值。同样地,Python内置的方法int() 也是返回整数。

那么问题来了,现在floor()int() 方法之间有什么区别?

下面的例子显示,当一个正数值通过时,输出结果是一样的。然而,当给出一个负数值时,这两个方法显示了不同的结果。

import math
def floor_function(number):
    return math.floor(number)
print("The floor method for a positive number is",floor_function(1.6))
print("The built-in int() method for a positive number is",int(1.6))
# int() and floor() difference can be seen with negative input value
print("The floor method for a negative number is",floor_function(-1.6))
print("The built-in int() method for a negative number is",int(-1.6))

输出:

The floor method for a positive number is 1
The built-in int() method for a positive number is 1
# different return value when input is a negative number
The floor method for a negative number is -2
The built-in int() method for a negative number is -1

示例代码:Python双斜线 (//) 操作符与地板除法的math.floor() 方法的比较

在Python中,双斜线// 操作符对数字进行分割,并输出底层浮点数。同样,如果我们使用除法运算符/ ,它只对两个数进行除法,然后我们使用floor() 方法来输出底数。

区别出现在两种方法的输出中,floor() 方法返回整数类型,而双斜线// 操作符返回浮点数。

import math
num1 = -10.1
num2 = -3.22
# double slash always returns float value
print("The result of the double slash operator is",num1//num2)
print("The result of the floor() method is",math.floor(num1/num2))

输出:

# double slash // operator returns a float number
The result of the double slash operator is 3.0
# floor() method returns an integer
The result of the floor() method is 3

示例代码:使用math.floor() 方法时,TypeError 的原因

math.floor() ,我们要求单个参数是一个数字值。如果我们传入除数值以外的任何东西,它就会返回一个TypeError

math.floor() 方法只接受整数或浮点数作为参数。

import math
# this function collects the incorrect data type for math.floor method
def data_type(value):
    return math.floor(value)
print("String data type is pass ",data_type("Hello"))

输出:

# method returns type error for the string data
TypeError: must be real number, not str