溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解Python3中ceil()函數(shù)用法

發(fā)布時(shí)間:2020-09-25 13:35:51 來源:腳本之家 閱讀:186 作者:laozhang 欄目:開發(fā)技術(shù)

描述

ceil(x) 函數(shù)返回一個(gè)大于或等于 x 的的最小整數(shù)。

語法

以下是 ceil() 方法的語法:

import math

math.ceil( x )

注意:ceil()是不能直接訪問的,需要導(dǎo)入 math 模塊,通過靜態(tài)對象調(diào)用該方法。

參數(shù)

x -- 數(shù)值表達(dá)式。

返回值

函數(shù)返回返回一個(gè)大于或等于 x 的的最小整數(shù)。

實(shí)例

以下展示了使用 ceil() 方法的實(shí)例:

#!/usr/bin/python3
import math  # 導(dǎo)入 math 模塊

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

以上實(shí)例運(yùn)行后輸出結(jié)果為:

math.ceil(-45.17) : -45
math.ceil(100.12) : 101
math.ceil(100.72) : 101
math.ceil(math.pi) : 4

python 向上取整ceil 向下取整floor 四舍五入round

#encoding:utf-8
import math

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

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

#四舍五入
print "\nround---"
print "round(2.3) => ", round(2.3)
print "round(2.6) => ", round(2.6)

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

運(yùn)行結(jié)果:

詳解Python3中ceil()函數(shù)用法

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI