Rounding a number to nearest even

bdsatish bdsatish at gmail.com
Fri Apr 11 07:27:06 EDT 2008


On Apr 11, 4:19 pm, cokofree... at gmail.com wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
>   answer = round(n)
>   # is answer now odd
>   if answer % 2:
>     return answer - 1
>   else:
>     return answer

It fails for negative numbers: For -2.5 it gives -4.0 as answer
whereas I expect -2.0



More information about the Python-list mailing list