[issue4707] round(25, 1) should return an integer, not a float
David W. Lambert
report at bugs.python.org
Wed Jan 28 04:45:25 CET 2009
David W. Lambert <lambertdw at corning.com> added the comment:
I'd prefer round(x,positive_integer) return float. Returning int is a
bit of a lie, except that the decimal module is available to avoid this
sort of lie.
For non-positive integer roundings I'd like an integer return.
In my opinion, we'd benefit from this definition of round:
import numbers
def round(a,p=0,base=10):
'''
>>> round(147,-1,5)
145.0
>>> round(143,-1,5)
145.0
>>> round(142,-1,5)
140.0
>>> round(12.345,1,2)
12.5
>>> round(12.345,2,2)
12.25
>>> round(12.345,-2,2)
12
>>> round(12.345,-3,2)
16
'''
# consider using sign transfer for negative a
if base < 1:
raise ValueError('base too confusing')
require_integral_output = (
(p < 1) and
isinstance(base, numbers.Integral) and
isinstance(p, numbers.Integral))
b = base**p
result = int(a*b+0.5)/b
if require_integral_output:
result = int(0.5+result)
return result
----------
nosy: +LambertDW
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4707>
_______________________________________
More information about the Python-bugs-list
mailing list