python中向上取整可以用ceil函數(shù),ceil函數(shù)是在math模塊下的一個(gè)函數(shù)。
向上取整需要用到math模塊中的ceil()方法:
>>>importmath
>>>math.ceil(3.25)
4.0
>>>math.ceil(3.75)
4.0
>>>math.ceil(4.85)
5.0
分別取整數(shù)部分和小數(shù)部分
有時(shí)候我們可能需要分別獲取整數(shù)部分和小數(shù)部分,這時(shí)可以用math模塊中的modf()方法,該方法返回一個(gè)包含小數(shù)部分和整數(shù)部分的元組:
>>>importmath
>>>math.modf(3.25)
(0.25,3.0)
>>>math.modf(3.75)
(0.75,3.0)
>>>math.modf(4.2)
(0.20000000000000018,4.0)
以上內(nèi)容為大家介紹了python培訓(xùn)之如何對(duì)一個(gè)數(shù)向上取整,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。