1

I was looking for a function which, given a float, will return an int of the float floored or rounded to the nearest int. Is there such a thing built-in or available in a module?

The following code does the trick but I avoid reinventing the wheel.

import math

def realround(number):
    _, d = divmod(number, 1)
    if d > 0.5:
        return int(math.ceil(number))
    else:
        return int(math.floor(number))

print(realround(12.3))
print(realround(14.5))
print(realround(15.8))
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 3
    https://www.google.co.uk/search?q=python%20round – Oliver Charlesworth Jul 06 '14 at 16:59
  • @OliCharlesworth: as you can hopefully imagine from my code, I have looked some time for that. Since I did write a function which performs the operation I was surprised there was not something built in. Pffff, downvoting because someone asks a question providing appropriate code is really for [censored] (I do not imply you downvoted, I am just pissed of by the mechanism) – WoJ Jul 06 '14 at 17:19

2 Answers2

2

It’s called round:

print(round(12.3))
print(round(14.5))
print(round(15.8))

It rounds to even numbers on .5, though.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • I just looked up the python documentation because I thought python used round-to-even, but it seems it doesn't: https://docs.python.org/2/library/functions.html#round – Axel Jul 06 '14 at 17:04
  • @Axel: It does in Python 3, which is a really horrible, subtle breaking change. (Well, it also returns an `int` in Python 3 when called with one argument.) =( – Ry- Jul 06 '14 at 17:05
  • Just saw that. I am so glad we didn't choose python... ;-) – Axel Jul 06 '14 at 17:07
  • 1
    Thanks. I do not know how I could have missed that. – WoJ Jul 06 '14 at 17:21
  • 2
    @Axel Apparently, rounding half up is falling out favour. The reason being that over many roundings there is a bias towards larger numbers. eg. `0.5 + 1.5` is equal to `2` whereas `round(0.5) + round(1.5)` would be `3`. The rounding type used in python3 results in `2`. See http://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior – Dunes Jul 06 '14 at 18:22
  • @Dunes For me, it's not so important whether it's round-to-even or round-away-from-zero, but to have consistency between releases. In our main application, rounding is used a lot, and having to fix hundreds of test cases or working around this when upgrading the development environment would be a nightmare. – Axel Jul 07 '14 at 06:10
  • @Axel if consistency is what you need then you only need redefine round rather than rewrite all your tests. You could even redefine the builtin round so that the change only needs to be in once place. eg. `import builtins; builtins.round = round_half_up`. That's probably not advisable though. – Dunes Jul 07 '14 at 12:43
  • @Axel, one doesn’t just upgrade from Python 2 to Python 3 and expect to make a few minor changes, though. They’re not compatible. The transition packages that exist would also fix this for you. – Ry- Jul 07 '14 at 15:36
  • @false: Unfortunately, the obvious candidate for a transition package (2to3) doesn't fix `round`. Perhaps it should. – Mark Dickinson Jul 08 '14 at 19:12
-2

Cast the value to an int and values to the right of the decimel point disappear (truncation).

print(int(12.3))
print(int(14.5))
print(int(15.8))