David W. Lambert <[email protected]> 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 <[email protected]>
<http://bugs.python.org/issue4707>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com