掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
內(nèi)置函數(shù)round()用于返回浮點(diǎn)數(shù),它是指定位數(shù)的給定十進(jìn)制數(shù)的舍入版本。

**round(number, ndigits)** #where number can be int,float.
round()參數(shù):接受兩個(gè)參數(shù)。如果默認(rèn)小數(shù)位數(shù)為 0,函數(shù)將返回最近的整數(shù)。
| 參數(shù) | 描述 | 必選/ 可選 |
|---|---|---|
| 數(shù)字 | 要舍入的數(shù)字 | 需要 |
| ndigits | 給定數(shù)字向上舍入的數(shù)字;默認(rèn)為 0 | 可選擇的 |
任何整數(shù)值對(duì) ndigits 都有效,即它的值可以是正的、零的或負(fù)的。如果小數(shù)點(diǎn)后的值> =5,則該值將被舍入到+1,否則它將返回該值,直到提到的小數(shù)位數(shù)。
| 投入 | 返回值 | | 如果沒(méi)有給出 ndigits。 | 返回給定數(shù)字的最近整數(shù)。 | | 如果 ndigits | 返回四舍五入到位數(shù)的數(shù)字 |
round()方法的示例round()在 Python 中是如何工作的? # for integers
print(round(10))
# for floating point
print(round(10.7))
# even choice
print(round(5.5))
輸出:
10
11
6
print(round(2.665, 2))
print(round(2.675, 2))
輸出:
2.67
2.67
class Data:
id = 0
def __init__(self, i):
self.id = i
def __round__(self, n):
return round(self.id, n)
d = Data(10.5234)
print(round(d, 2))
print(round(d, 1))
輸出:
10.52
10.5 
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流