Skip to content

Commit a4911b9

Browse files
committed
#322 Add support for negative ndigits in round; additionally, fixing a bug so that it handles passing in Decimal properly
1 parent 01a1d31 commit a4911b9

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/future/builtins/newround.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
``python-future``: pure Python implementation of Python 3 round().
33
"""
44

5+
from __future__ import division
56
from future.utils import PYPY, PY26, bind_method
67

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

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

3635
if PYPY:
@@ -42,15 +41,19 @@ def newround(number, ndigits=None):
4241
d = number
4342
else:
4443
if not PY26:
45-
d = Decimal.from_float(number).quantize(exponent,
46-
rounding=ROUND_HALF_EVEN)
44+
d = Decimal.from_float(number)
4745
else:
48-
d = from_float_26(number).quantize(exponent, rounding=ROUND_HALF_EVEN)
46+
d = from_float_26(number)
47+
48+
if ndigits < 0:
49+
result = newround(d / exponent) * exponent
50+
else:
51+
result = d.quantize(exponent, rounding=ROUND_HALF_EVEN)
4952

5053
if return_int:
51-
return int(d)
54+
return int(result)
5255
else:
53-
return float(d)
56+
return float(result)
5457

5558

5659
### From Python 2.7's decimal.py. Only needed to support Py2.6:

tests/test_future/test_builtins.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ def test_round(self):
146146
self.assertTrue(isinstance(round(123.5, 0), float))
147147
self.assertTrue(isinstance(round(123.5), Integral))
148148

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

0 commit comments

Comments
 (0)