File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change 2
2
``python-future``: pure Python implementation of Python 3 round().
3
3
"""
4
4
5
+ from __future__ import division
5
6
from future .utils import PYPY , PY26 , bind_method
6
7
7
8
# Use the decimal module for simplicity of implementation (and
@@ -29,8 +30,6 @@ def newround(number, ndigits=None):
29
30
if hasattr (number , '__round__' ):
30
31
return number .__round__ (ndigits )
31
32
32
- if ndigits < 0 :
33
- raise NotImplementedError ('negative ndigits not supported yet' )
34
33
exponent = Decimal ('10' ) ** (- ndigits )
35
34
36
35
if PYPY :
@@ -42,15 +41,19 @@ def newround(number, ndigits=None):
42
41
d = number
43
42
else :
44
43
if not PY26 :
45
- d = Decimal .from_float (number ).quantize (exponent ,
46
- rounding = ROUND_HALF_EVEN )
44
+ d = Decimal .from_float (number )
47
45
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 )
49
52
50
53
if return_int :
51
- return int (d )
54
+ return int (result )
52
55
else :
53
- return float (d )
56
+ return float (result )
54
57
55
58
56
59
### From Python 2.7's decimal.py. Only needed to support Py2.6:
Original file line number Diff line number Diff line change @@ -146,7 +146,6 @@ def test_round(self):
146
146
self .assertTrue (isinstance (round (123.5 , 0 ), float ))
147
147
self .assertTrue (isinstance (round (123.5 ), Integral ))
148
148
149
- @unittest .skip ('negative ndigits not implemented yet' )
150
149
def test_round_negative_ndigits (self ):
151
150
self .assertEqual (round (10.1350 , 0 ), 10.0 )
152
151
self .assertEqual (round (10.1350 , - 1 ), 10.0 )
You can’t perform that action at this time.
0 commit comments