1. 程式人生 > >python學習筆記:取整函式

python學習筆記:取整函式

三種取整方式:

一、向下取整:即捨去小數點後所有資料。int (n),例如:

int(3.67)     #figure out 3

二、四捨五入:round(n),例如:

round(4.56)     #figure out 5
round(-4.56)    #figure out -5
round(-4.23)    #figure out -4

**round函式並不精確,問題詳見“python筆記:round()函式及相關

三、向上取整:即捨去小數點後所有資料,並向上進1。import math  math.ceil(n),例如:

import math
math.ceil(5.44)    #figure out 6
math.ceil(5.56)    #figure out 6
math.ceil(-4.56)   #figure out -4