int() not working for floats?
<p>I recently ran into this in Python 3.5:</p>

<pre><code>&gt;&gt;&gt; flt = '3.14'
&gt;&gt;&gt; integer = '5'
&gt;&gt;&gt; float(integer)
5.0
&gt;&gt;&gt; float(flt)
3.14
&gt;&gt;&gt; int(integer)
5
&gt;&gt;&gt; int(flt)
Traceback (most recent call last):
  File "&lt;pyshell#7&gt;", line 1, in &lt;module&gt;
    int(flt)
ValueError: invalid literal for int() with base 10: '3.14'
</code></pre>

<p>Why is this? It seems like it should return <code>3</code>. Am I doing something wrong, or does this happen for a good reason?</p>

<p>The other answers already give you a good explanation about your issue, another way to understand what's going would be doing something like this:</p>

<pre><code>import sys

for c in ['3.14', '5']:
    try:
        sys.stdout.write(
            "Casting {0} {1} to float({0})...".format(c, c.__class__))
        value = float(c)
        sys.stdout.write("OK -&gt; {0}\n".format(value))
        print('-' * 80)
    except:
        sys.stdout.write("FAIL\n")

    try:
        sys.stdout.write(
            "Casting {0} {1} to int({0})...".format(c, c.__class__))
        value = int(c)
        sys.stdout.write("OK -&gt; {0}\n".format(value))
    except:
        sys.stdout.write("FAIL\n")
        sys.stdout.write("Casting again using int(float({0}))...".format(value))
        value = int(float(c))
        sys.stdout.write("OK -&gt; {0}\n".format(value))
        print('-' * 80)
</code></pre>

<p>Which outputs:</p>

<pre><code>Casting 3.14 &lt;class 'str'&gt; to float(3.14)...OK -&gt; 3.14
--------------------------------------------------------------------------------
Casting 3.14 &lt;class 'str'&gt; to int(3.14)...FAIL
Casting again using int(float(3.14))...OK -&gt; 3
--------------------------------------------------------------------------------
Casting 5 &lt;class 'str'&gt; to float(5)...OK -&gt; 5.0
--------------------------------------------------------------------------------
Casting 5 &lt;class 'str'&gt; to int(5)...OK -&gt; 5
</code></pre>

python python-3.x floating-point int