I've been playing around with python for a bit now and i've noticed a strange behavior that makes me curious: what is the difference between float(int(n))
and round(n)
?
When should I use one, another or neither of them?
Note: The python implementation changed between 2.7 and 3.x. I corrected the answer accordingly.
For the sake of completeness, let me add two more functions to your question and explain the differences between float(int(x))
, math.floor(x)
, round(x)
and math.ceil(x)
.
Let's start with a question: "What integer represents best the number 1.6?" We have two possible answers (1 and 2) but many different reasons why one answer may be better than the other one:
int(1.6)==1
: This is what you get when you cut off the decimals.math.floor(1.6)==1
: It's less than 2. Incomplete pieces don't count.round(1.6)==2
: Because 2 is closer than 1.math.ceil(1.6)==2
: It's more than 1. When you start a part, you have to pay the full price.Let's ask python to print a nice table of the results you get with different values of x:
from math import floor, ceil
tab='\t'
print('x \tint\tfloor\tround\tceil')
for x in (
1.0, 1.1, 1.5, 1.9, -1.1, -1.5, -1.9,
-2.5, -1.5, -0.5, 0.5, 1.5, 2.5,
):
print(x, int(x), floor(x), round(x), ceil(x), sep=tab)
Here is the output:
x int floor round ceil
1.0 1 1 1 1
1.1 1 1 1 2
1.5 1 1 2 2
1.9 1 1 2 2
-1.1 -1 -2 -1 -1
-1.5 -1 -2 -2 -1
-1.9 -1 -2 -2 -1
-2.5 -2 -3 -2 -2
-1.5 -1 -2 -2 -1
-0.5 0 -1 0 0
0.5 0 0 0 1
1.5 1 1 2 2
2.5 2 2 2 3
You see that none of these four functions are equivalent.
floor
rounds towards minus infinity: It chooses always the lowest possible answer: floor(1.99)==1
and floor(-1.01)==-2
.ceil
rounds towards infinity: It chooses always the highest possible answer: ceil(1.01)==2
and ceil(-1.99)=-1
.int
rounds towards zero: For positive x
it is like floor
, for negative x
it is like ceil
.round
rounds to the closest possible solution: round(1.49)=1
and round(1.51)==2
. When x
is exactly at the midpoint of two consecutive integers, round(x)
will be the closest even number.
round(1.5)==2
and round(2.5)==2
. This is called half to even rounding or Banker's Rounding because it is commonly used in financial calculations.Note: The python implementation changed between 2.7 and 3.x: Python 2.7 does not use the "half-to-even rounding" rule (explained above) but rounds all half-numbers away from zero: round(1.5)==2
and round(-1.5)==-2
. Bankers and mathematicians who care about this agree that the "half to even rounding" rule used in 3.x is the "right way" to do it because it distributes rounding errors fairly.
int(x)
as rounding, it is dropping everything after the decimal point which is technically known as truncation
(although you are correct that the effect is identical to round-toward-zero). Just thought I would mention this to avoid confusion with traditional concepts of rounding. (note: int(x)
is identical to math.trunc(x)
as long as x
is numeric)
round(0.5)==0
but int(0.5+0.5)==1
; floor(-1.9)==-2
but int(-1.9)==-1
; ceil(1)==1
but int(1)+1==2
. Python has the four similar but subtly different functions because in different situations, you need different kinds of rounding. I suggest you first understand the different intentions between the four functions (as explained in the answer) and then always choose the function that does what you actually want.
Commented
Mar 31, 2022 at 14:00
x // 1
, which my testing tells me behaves like floor()
.
Commented
Dec 20, 2023 at 11:01
// 1
is __floordiv__
which first divides by 1 (and thus returns the same number) and then applies floor()
.
Commented
Dec 20, 2023 at 16:10
round(n)
is a function to round a float, int(n)
will cast a float to an integer and will get rid of the decimal part by truncating it.
int(-3.6)
will give -3 and round(-3.6)
will give -4 so still rounding for round and truncating for int().
float(int(n))
will have the same behaviour than int(n) but will cast it from an int
to a float
.
type(round(np.float64(2.000054))) returns <class 'numpy.float64'>
thus it is necessary to be safe and use int(round())
which always works. For me, this is a python bug.
type(round(np.float64(2.000054)))
returns <class 'int'>
, maybe it is an issue with older versions of Python?
round
is a mathematical rounding and int
just casts to an integer, essentially truncating the value.
Take 2 variables:
var1 = 5.5
var2 = 5.1
If we round them
round(var1)
round(var2)
We get 6.0 and 5.0 respectively.
However, if we just cast them to an int
int(var1)
int(var2)
We get 5 for both of them.
You can test this out yourself in the python interpreter.
round()
without a second argument returns an int
in Py3
Commented
Apr 27, 2017 at 14:44
round
rounds down 4.5
, which makes it essentially the same as casting to an int
round()
will prefer the even
number for 2 equally distance results, round(4.5) -> 4
and round(3.5) -> 4
. Note also: as per the documentation "the behavior of round() for floats can be surprising" - a limitation of floating point arithmetic.
Commented
Apr 27, 2017 at 15:03
round
can also take a second argument for the precision. That makes it different from thefloat(int(n))
. As for the neither now, that is too broad.round(1.7)
andfloat(int(1.7))
?