Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does round(5/2) return 2?

Using python 3.4.3,

round(5/2) # 2

Shouldn't it return 3?

I tried using python 2 and it gave me the correct result

round(5.0/2) # 3

How can I achieve a correct rounding of floats?

like image 371
Afonso Matos Avatar asked Jun 16 '15 13:06

Afonso Matos


1 Answers

if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).

Quoting the documentation for the round function. Hope this helps :)

On a side note, I would suggest always read the doc when you face this kind of question (haha)

like image 146
laugri Avatar answered Sep 23 '22 02:09

laugri