python中向上取整可以用ceil函數(shù),ceil函數(shù)是在math模塊下的一個函數(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ù)部分,這時可以用math模塊中的modf()方法,該方法返回一個包含小數(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培訓之如何對一個數(shù)向上取整,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。