Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#322 Add support for negative ndigits in round; additionally, fixing … #535

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/future/builtins/newround.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
``python-future``: pure Python implementation of Python 3 round().
"""

from __future__ import division
from future.utils import PYPY, PY26, bind_method

# Use the decimal module for simplicity of implementation (and
Expand Down Expand Up @@ -29,8 +30,6 @@ def newround(number, ndigits=None):
if hasattr(number, '__round__'):
return number.__round__(ndigits)

if ndigits < 0:
raise NotImplementedError('negative ndigits not supported yet')
exponent = Decimal('10') ** (-ndigits)

if PYPY:
Expand All @@ -42,15 +41,19 @@ def newround(number, ndigits=None):
d = number
else:
if not PY26:
d = Decimal.from_float(number).quantize(exponent,
rounding=ROUND_HALF_EVEN)
d = Decimal.from_float(number)
else:
d = from_float_26(number).quantize(exponent, rounding=ROUND_HALF_EVEN)
d = from_float_26(number)

if ndigits < 0:
result = newround(d / exponent) * exponent
else:
result = d.quantize(exponent, rounding=ROUND_HALF_EVEN)

if return_int:
return int(d)
return int(result)
else:
return float(d)
return float(result)


### From Python 2.7's decimal.py. Only needed to support Py2.6:
Expand Down
1 change: 0 additions & 1 deletion tests/test_future/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def test_round(self):
self.assertTrue(isinstance(round(123.5, 0), float))
self.assertTrue(isinstance(round(123.5), Integral))

@unittest.skip('negative ndigits not implemented yet')
def test_round_negative_ndigits(self):
self.assertEqual(round(10.1350, 0), 10.0)
self.assertEqual(round(10.1350, -1), 10.0)
Expand Down