1

I am trying to take the round of float number but in python for different float values it gives the different result. Example : -

>>> round(4.5)
4

and

>>> round(3.5)
4

as you can see the result round(4.5) return 4 while rount(3.5) return 4. How to solve this problem. ? I want round(4.5) to return 5. Please help me out. Thanks

rofelia09
  • 51
  • 11
  • 1
    Possible duplicate of [Python 3.x rounding behavior](https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) – deadvoid Oct 18 '18 at 04:35

2 Answers2

2

See official doc.

Basically it called even round. It will round x.5 value into even choice.

And here is why.

Sraw
  • 18,892
  • 11
  • 54
  • 87
0

From docs:

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

I.e. Python 3 does "rounding half to even".

You can do "round up" this way:

import math
def round_up(x):
    return math.floor(x + 0.5)
Amadan
  • 191,408
  • 23
  • 240
  • 301