Rounding a number to nearest even
bdsatish
bdsatish at gmail.com
Fri Apr 11 07:42:22 EDT 2008
On Apr 11, 4:37 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> bdsatish wrote:
> > The built-in function round( ) will always "round up", that is 1.5 is
> def rounded(v):
> rounded = round(v)
> if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1:
> if v > 0:
> return rounded - 1
> return rounded + 1
> return rounded
>
> last = None
> for n in range(-29, 28):
> x = n * .25
> r = xr(x)
> if r != last:
> last = r
> print
> print '%s->%s' % (x, xr(x)),
>
Hi Scott,
This is what I was looking for.. I forgot about divmod( ) thanks for
reminding.
More information about the Python-list
mailing list