天天看點

Python函數之 ceil, floor,round

Ceiling是向上取整

floor是向下取整

Round是四舍五入的

ceil

描述

ceil() 函數傳回數字的上入整數

ceil():将小數部分一律向整數部分進位。

#!/usr/bin/python
import math   # This will import math module

print "math.ceil(-45.17) : ", math.ceil(-)
print "math.ceil(100.12) : ", math.ceil()
print "math.ceil(100.72) : ", math.ceil()
print "math.ceil(119L) : ", math.ceil(L)
print "math.ceil(math.pi) : ", math.ceil(math.pi)
           

輸出結果為:

math.ceil(-) :  -
math.ceil() :  
math.ceil() :  
math.ceil(L) :  
math.ceil(math.pi) : 
           
#向上取整
print "math.ceil---"
print "math.ceil(2.3) => ", math.ceil()
print "math.ceil(2.6) => ", math.ceil()
           

floor

floor():一律舍去,僅保留整數

math.floor()// 傳回12
math.floor()//傳回12
math.floor()//傳回12
           
#向下取整
print "\nmath.floor---"
print "math.floor(2.3) => ", math.floor()
print "math.floor(2.6) => ", math.floor()
           

round

round():進行四舍五入

math.round()// 傳回12
math.round()//傳回13
math.round()//傳回12 
           
#encoding:utf-
import math

#這三個的傳回結果都是浮點型
print "\n\nNOTE:every result is type of float"
print "math.ceil(2) => ", math.ceil()
print "math.floor(2) => ", math.floor()
print "round(2) => ", round()