Python 的 round(~)
方法返回四舍五入到指定數字精度的輸入數字。
參數
1. number
| number
要執行舍入的數字。
2. ndigits
| number
| optional
四舍五入到小數點後的位數精度。默認為 0。
返回值
返回四舍五入到指定數字精度的輸入數字。
例子
基本用法
將 3.5
四舍五入到最接近的整數:
round(3.5)
4
ndigits 參數
要將 4.6854
舍入為 3
小數點後的數字精度:
round(4.6854, 3)
4.685
我們可以為 ndigits
傳遞一個負值以舍入小數點前:
round(123456, -2)
123500
注意
通過傳遞ndigits=-2
,我們四舍五入到最接近的百位。
相關用法
- Python round()用法及代碼示例
- Python NumPy roll方法用法及代碼示例
- Python NumPy rot90方法用法及代碼示例
- Python NumPy roots方法用法及代碼示例
- Python random.getstate()用法及代碼示例
- Python random.triangular()用法及代碼示例
- Python Numpy recarray.tostring()用法及代碼示例
- Python reduce()用法及代碼示例
- Python response.status_code用法及代碼示例
- Python Numpy recarray.tobytes()用法及代碼示例
- Python string rpartition()用法及代碼示例
- Python numpy random.mtrand.RandomState.randn用法及代碼示例
- Python randint()用法及代碼示例
- Python numpy random.mtrand.RandomState.rand用法及代碼示例
- Python Numpy recarray.min()用法及代碼示例
- Python response.request用法及代碼示例
- Python repr方法用法及代碼示例
- Python Numpy recarray.cumprod()用法及代碼示例
- Python numpy random.mtrand.RandomState.pareto用法及代碼示例
- Python re.compile用法及代碼示例
- Python NumPy remainder方法用法及代碼示例
- Python Django re_path用法及代碼示例
- Python response.elapsed用法及代碼示例
- Python numpy random.mtrand.RandomState.standard_normal用法及代碼示例
- Python response.cookies用法及代碼示例
注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 Python | round method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。