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 22``python-future``: pure Python implementation of Python 3 round().
33"""
44
5+ from __future__ import division
56from 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:
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments