XueXianqi
246 天前
加,都加,入参出参的 Type Hints 、doc-string 、Demo 都加上
举个栗子:
```python
from decimal import Decimal
from typing import Union
DOT: str = "."
ERROR_TIP: str = "numbers 元素必须为数字类型"
def x_round(
num: Union[int, float, Decimal],
bit: int = 2,
) -> Decimal:
"""
提高精度的四舍五入
>>> round(num=1.115) # no
1.11
>>> round(num=1.125) # yes
1.13
>>> x_round(num=1.115) # yes
1.12
>>> x_round(num=1.125) # yes
1.13
:param num: 需要四舍五入(严格意义上,是:四舍六入五成双)的数字
:param bit: 精确到小数点后几位
:return: 四舍五入后的数字
:raise ValueError: bit 必须大于等于 0
"""
if bit < 0:
raise ValueError("bit 必须大于等于 0")
_times: int = 10 ** bit # 倍数(功能等同于保留几位小数)
rough_num: float = round(num * _times) / _times
return Decimal(str(rough_num))
```