Python 3 内置函数 - round
函数
0. round()
函数
返回浮点数的四舍六入值。精度要求高的运算,不建议使用该函数。
- 参数:ndigits: 小数点位数。
1. 使用方法
>>> help(round)
# output:
Help on built-in function round in module builtins:
## 使用方法:
round(number, ndigits=None)
Round a number to a given precision in decimal digits.
The return value is an integer if ndigits is omitted or None. Otherwise
the return value has the same type as the number. ndigits may be negative.
2. 使用示例
示例1.
>>> round(111.123)
# output:
111
示例2.
>>> round(1111.12345, 3)
# output:
1111.123
示例3.
>>> round(1111.12345, 4)
# output:
1111.1235
>>> round(111.12355, 4)
# output:
111.1235 # 与我们期望的不一致。
>>> round(111.123556, 4)
# output:
111.1236
3. 对比 numpy
的 round()
函数
import numpy as np
np.round(111.12355, 4)
# output:
111.1236 # 和我们期望的一致。