>>> round(1.5)

2.0

>>> round(2.5)

3.0

>>> round(2.5)

2

>>>

>>> round(2.5)

2

why

Round() problems

0

It’s not a bug, Python 3 defaults to round .5 to the *nearest neighbour that is even* - .5 is exactly inbetween two values, so which side should it round to? Rounding up to the next integer is just one way to do it.

Plus be aware that you’re rounding a floating point number [to the nearest even integer]. If the last digit is 5, it’ll be stored as `4999999999999...`

. Floating point is an approximation, most decimal numbers cannot be represented exactly in floating point. That’s the way it’s stored.

If you want specific rounding behaviour, then you either need to use the decimal module, or write a function that rounds how you would like it to round. + If you just want the integer part, `floor`

gets you that. If you want the next integer, `ceil`

. Don’t rely on round to do something specific you’re expecting from being taught a certain way of rounding, basically *(also, don’t use floating point for financial calculations if that happens to be what you’re trying to do)*

0 Likes