floor()方法返回不大于x的最大整數(shù)(向下取整)。
語法
以下是floor()方法的語法:
1
2
3
|
import math math.floor( x ) |
注意:此函數(shù)是無法直接訪問的,所以我們需要導(dǎo)入math模塊,然后需要用math的靜態(tài)對象來調(diào)用這個函數(shù)。
參數(shù)
- x -- 這是一個數(shù)值表達(dá)式。
返回值
此方法返回不大于x的最大整數(shù)。
例子
下面的例子顯示了floor()方法的使用。
1
2
3
4
5
6
7
8
|
#!/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) |
當(dāng)我們運行上面的程序,它會產(chǎn)生以下結(jié)果:
1
2
3
4
5
|
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 |