Author LambertDW
Recipients LambertDW, ajaksu2, dingo, giampaolo.rodola, gvanrossum, jyasskin, loewis, mark.dickinson, orsenthil, rhettinger
Date 2009-01-28.03:45:22
SpamBayes Score 1.81777e-08
Marked as misclassified No
Message-id <1233114325.2.0.0422156047594.issue4707@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2009-01-28 03:45:25LambertDWsetrecipients: + LambertDW, gvanrossum, loewis, rhettinger, mark.dickinson, orsenthil, giampaolo.rodola, ajaksu2, jyasskin, dingo
2009-01-28 03:45:25LambertDWsetmessageid: <1233114325.2.0.0422156047594.issue4707@psf.upfronthosting.co.za>
2009-01-28 03:45:23LambertDWlinkissue4707 messages
2009-01-28 03:45:22LambertDWcreate