32

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?

5
  • In 1st case you first cast the number to int so all digits after comma are gone then you cast again to float so for example 3.6 will result in 3.0 which is mathematically wrong. Round is mathematical round
    – cymruu
    Commented Apr 27, 2017 at 14:40
  • The round can also take a second argument for the precision. That makes it different from the float(int(n)). As for the neither now, that is too broad.
    – Ma0
    Commented Apr 27, 2017 at 14:40
  • 3
    Have you tried something like: round(1.7) and float(int(1.7))? Commented Apr 27, 2017 at 14:42
  • What is the strange behaviour you've noticed? Commented Apr 27, 2017 at 14:47
  • @Veltro same results for what numbers? Update the question Commented Apr 27, 2017 at 15:08

3 Answers 3

57

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.

5
  • 3
    Great answer. My only nit-pick would be that programmers don't view 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)
    – Cole
    Commented Aug 5, 2021 at 13:57
  • generally speaking: int(x) => removes the decimal/fractional part and returns the integer part of x; round() => int(x + 0.5); floor(x) => int(x); ceil(x) => int(x) + 1
    – ReignBough
    Commented Mar 31, 2022 at 9:22
  • 2
    @ReignBough: Watch out: All your equations are imprecise --- the rules are more complicated than you think: 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
  • 1
    With Python 3.x the other option is x // 1, which my testing tells me behaves like floor(). Commented Dec 20, 2023 at 11:01
  • @DonovanBaarda -- You are right: // 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
12

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.

5
  • What about negative numbers? Commented Apr 27, 2017 at 14:48
  • 1
    int(-3.6) will give -3 and round(-3.6) will give -4 so still rounding for round and truncating for int().
    – Silveris
    Commented Apr 27, 2017 at 14:52
  • @Veltro float(int(n)) will have the same behaviour than int(n) but will cast it from an int to a float.
    – Silveris
    Commented Apr 28, 2017 at 7:07
  • Unfortunately, it is not always true that round(num) will create an int. if num is numpy.float64, it does not convert to int. 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.
    – Ray Lutz
    Commented Sep 6, 2022 at 20:08
  • Not sure which version of Python you are using, but I just tested with Python 3.6, 3.7 and 3.8 and type(round(np.float64(2.000054))) returns <class 'int'>, maybe it is an issue with older versions of Python?
    – Silveris
    Commented Sep 7, 2022 at 9:39
6

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.

6
  • Note: round() without a second argument returns an int in Py3
    – AChampion
    Commented Apr 27, 2017 at 14:44
  • The question was tagged python2, so I didn't check python3..but I didn't actually know that, so thanks (I still use mostly python 2 on a daily basis)
    – Luke K
    Commented Apr 27, 2017 at 14:47
  • What about negative numbers? Commented Apr 27, 2017 at 14:48
  • That's another difference to note then. Unlike in maths and python2, python3 round rounds down 4.5, which makes it essentially the same as casting to an int
    – Luke K
    Commented Apr 27, 2017 at 14:54
  • 1
    Yes, there is a difference between mathematical rounding and python rounding, in that python's 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.
    – AChampion
    Commented Apr 27, 2017 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.