当round
从未来导入时,它的行为与 Python3round
函数不同。具体来说,它不支持负数舍入。
在 Python3 中:
>>> round(4781, -2)
4800
在 Python2 中:
>>> from builtins import round
>>> round(4781, -2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/future/builtins/newround.py", line 33, in newround
raise NotImplementedError('negative ndigits not supported yet')
NotImplementedError: negative ndigits not supported yet
可能的解决方案包括负舍入的错误处理、编写我自己的round
函数等。应该如何处理?(隐含地,我要求最佳实践,大多数 Pythonic,被社区接受等)