8

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?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Afonso Matos
  • 2,406
  • 1
  • 20
  • 30
  • This is documented behavior in python. It differs between Python3 and Python2. – Willem Van Onsem Jun 16 '15 at 13:22
  • Since `5` and `2` are both integers, `5/2` is also an integer in Python 2.x, but in Python 3.x it should give you what you expect. I suspect you got the Python 2/3 backwards. – Gabe Jun 16 '15 at 13:25
  • 1
    This is often called [Bankers' Rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even). – Phylogenesis Jun 16 '15 at 13:28
  • possible duplicate of [Python 3.x rounding behavior](http://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) – Peter Wood Jun 16 '15 at 13:29
  • Thanks for the reference, @Phylogenesis! I didn't know this. – Deacon Jun 16 '15 at 13:34

3 Answers3

7

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)

laugri
  • 597
  • 6
  • 13
5

Rounding toward even is correct behavior for Python 3. According to the Python 3 documentation for round():

...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)

Since 2.5 is equally close to 2 and 3, it rounds down to 2.

In Python 2, the docs for round() state:

...if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0)

Since 2.5 is equally close to 2 and 3, it rounds up to 3 (away from zero).

If you want to control how numbers round, the best way to do it might be the way I learned to round numbers back in my Applesoft BASIC days:

10 X = 5
15 PRINT "ROUND(" X "/2) = " (INT((X/2)+0.5))
20 X = 4.99
25 PRINT "ROUND(" X "/2) = " (INT((X/2)+0.5))

Umm...make that:

>>> x = 5 / 2
>>> print(x)
2.5
>>> y = int(x + 0.5)
>>> print(y)
3
>>> x = 4.99 / 2
>>> print(x)
2.495
>>> y = int(x + 0.5)
>>> print(y)
2
>>>
Deacon
  • 3,615
  • 2
  • 31
  • 52
  • 1
    Many old BASIC dialects I know round down on `INT()`, including negative numbers. I don't know Applesoft BASIC, but if http://www.calormen.com/jsbasic/ is accurate, it does, too. So Python `int(x + 0.5)` is only equivalent to many olden days BASIC `INT(X + 0.5)` for positive numbers. In Python you could use `math.floor(x + 0.5)`. – blubberdiblub Sep 17 '15 at 04:24
0

From doc

round(number[, ndigits]) -> number Round a number to a given precision in decimal digits (default 0 digits).This returns an int when called with one argument, otherwise the same type as the number. ndigits may be negative.

So

>>>round(5/2)
2
>>>round(5.0/2, 1)
2.5
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49