-7

The way the python int()function works rounds a number to the smallest value.

Such that 4.99 = 4

What can I do if I want 4.99 be equal to 5?

Juanvulcano
  • 1,354
  • 3
  • 26
  • 44
  • 2
    If you can deal with bankers rounding, using [`round`](http://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) – NightShadeQueen Jul 17 '15 at 22:10
  • 7
    Google "python round to nearest integer". Did you do any research before posting? The answer is *trivially* easy to find. – John Coleman Jul 17 '15 at 22:11

3 Answers3

5

int() truncates float values, removing the non-integral portion of the number. You are looking for rounding instead.

You can use the round() function for that:

>>> round(4.99)
5
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
5

There is a round() builtin:

>>> round(4.99)
5
fferri
  • 18,285
  • 5
  • 46
  • 95
0

math.ceil and math.floor return floats which are the nearest whole numbers above or below the parameter.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99