Message80694
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 |
|
Date |
User |
Action |
Args |
2009-01-28 03:45:25 | LambertDW | set | recipients:
+ LambertDW, gvanrossum, loewis, rhettinger, mark.dickinson, orsenthil, giampaolo.rodola, ajaksu2, jyasskin, dingo |
2009-01-28 03:45:25 | LambertDW | set | messageid: <1233114325.2.0.0422156047594.issue4707@psf.upfronthosting.co.za> |
2009-01-28 03:45:23 | LambertDW | link | issue4707 messages |
2009-01-28 03:45:22 | LambertDW | create | |
|