0
点赞
收藏
分享

微信扫一扫

Python 3 内置函数 - `round()`函数

热爱生活的我一雷广琴 2022-03-24 阅读 100
python

Python 3 内置函数 - round函数

0. round()函数

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. 对比 numpyround() 函数

import numpy as np
np.round(111.12355, 4)

# output:
111.1236   # 和我们期望的一致。

举报

相关推荐

0 条评论