Python做量化round()函数知道这些就够了
round()是一个独立函数,返回浮点数的四舍五入值。
round(number, digits)
举个例子
print(round(2.356, 2))
>> 2.36
print(round(2.354, 2))
>> 2.35
# PYTHON 2版本中
print(round(2.355, 2))
>> 2.36
# PYTHON 3版本中
print(round(2.355, 2))
>> 2.35
在python2.7的doc中,round()的最后写着,“Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0.” 保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。
但是到了python3.5的doc中,文档变成了"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice." 如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。
- Python 中关于 round 函数的小坑
.
.
.
2019-03-15 22:21:48写于杭州
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
