简单介绍Python中的floor()方法

Tanisha ·
更新时间:2024-09-21
· 868 次阅读

 floor()方法返回不大于x的最大整数(向下取整)。
语法

以下是floor()方法的语法:

import math math.floor( x )

注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数。
参数

    x -- 这是一个数值表达式。

返回值

此方法返回不大于x的最大整数。
例子

下面的例子显示了floor()方法的使用。

#!/usr/bin/python import math # This will import math module print "math.floor(-45.17) : ", math.floor(-45.17) print "math.floor(100.12) : ", math.floor(100.12) print "math.floor(100.72) : ", math.floor(100.72) print "math.floor(119L) : ", math.floor(119L) print "math.floor(math.pi) : ", math.floor(math.pi)

当我们运行上面的程序,它会产生以下结果:

math.floor(-45.17) : -46.0 math.floor(100.12) : 100.0 math.floor(100.72) : 100.0 math.floor(119L) : 119.0 math.floor(math.pi) : 3.0 您可能感兴趣的文章:Python中比较特别的除法运算和幂运算介绍低版本中Python除法运算小技巧跟老齐学Python之啰嗦的除法Python对小数进行除法运算的正确方法示例Python中除法使用的注意事项Python 除法小技巧python通过floor函数舍弃小数位的方法Python3中的真除和Floor除法用法分析



方法 floor Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号