Where is the correct round() method?

Paul McGuire ptmcg at austin.rr.com
Mon Jul 28 04:51:46 CEST 2008


On Jul 27, 8:55 pm, Larry Bates <larry.ba... at websafe.com`> wrote:
> josh logan wrote:
> > Hello,
>
> > I need a round function that _always_ rounds to the higher integer if
> > the argument is equidistant between two integers. In Python 3.0, this
> > is not the advertised behavior of the built-in function round() as
> > seen below:
>
> >>>> round(0.5)
> > 0
> >>>> round(1.5)
> > 2
> >>>> round(2.5)
> > 2
>
> > I would think this is a common need, but I cannot find a function in
> > the Python library to do it. I wrote my own, but did I miss such a
> > method in my search of the Python library?
>
> > Thanks
>
> I think what you want is something like:
>
> math.ceil(x-0.4999999999999)
>
> -Larry- Hide quoted text -
>
> - Show quoted text -

The version I learned back in my FORTRAN days was:

    int(x + 0.5)

-- Paul



More information about the Python-list mailing list