當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python 3 Number round()用法及代碼示例


描述

round()是 Python 中的 內置 函數。它返回從小數點開始四舍五入為 n 位的 x。

用法

以下是語法round()方法 -

round( x [, n]  )

參數

x- 這是一個數字表達式。

n- 表示從小數點到 x 要四舍五入的位數。默認值為 0。

返回值

此方法返回從小數點開始四舍五入為 n 位的 x。

示例

下麵的例子展示了 round() 方法的用法。

#!/usr/bin/python3

print ("round(70.23456):", round(70.23456))
print ("round(56.659,1):", round(56.659,1))
print ("round(80.264, 2):", round(80.264, 2))
print ("round(100.000056, 3):", round(100.000056, 3))
print ("round(-100.000056, 3):", round(-100.000056, 3))

輸出

當我們運行上述程序時,它會產生以下結果 -

round(70.23456): 70
round(56.659,1): 56.7
round(80.264, 2): 80.26
round(100.000056, 3): 100.0
round(-100.000056, 3): -100.0

相關用法


注:本文由純淨天空篩選整理自 Python 3 - Number round() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。