Python中的取整函數


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))

# 這三個的返回結果都是浮點型

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))

結果:
math.ceil---
math.ceil(2.3) =>  3
math.ceil(2.6) =>  3

math.floor---
math.floor(2.3) =>  2
math.floor(2.6) =>  2

round---
round(2.3) =>  2
round(2.6) =>  3

部分函數:
abs(number),返回數字的絕對值

cmath.sqrt(number),返回平方根,也可以應用於負數

float(object),把字符串和數字轉換為浮點數

help(),提供交互式幫助

input(prompt),獲取用戶輸入

int(object),把字符串和數字轉換為整數

math.ceil(number),返回數的上入整數,返回值的類型為浮點數

math.floor(number),返回數的下舍整數,返回值的類型為浮點數

math.sqrt(number),返回平方根不適用於負數

pow(x,y[.z]),返回X的y次冪(有z則對z取模)

repr(object),返回值的字符串標示形式

round(number[.ndigits]),根據給定的精度對數字進行四舍五入

str(object),把值轉換為字符串


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM