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><code>int()</code> expects an number or string that contains an integer literal. Per the <a href="https://docs.python.org/3/library/functions.html#int" rel="nofollow">Python 3.5.2</a> documentation:</p>

<blockquote>
  <p>If <em>x</em> is not a number or if <em>base</em> is given, then <strong><em>x</em> must be a string, <code>bytes</code>, or <code>bytearray</code> instance representing an integer literal</strong> in <em>radix base</em>. (Emphasis added)</p>
</blockquote>

<p>Meaning <code>int()</code> can only convert strings that contain integers. You can easily do this:</p>

<pre><code>&gt;&gt;&gt; flt = '3.14'
&gt;&gt;&gt; int(float(flt))
3
</code></pre>

<p>This will convert <code>flt</code> into a float, which is then valid for <code>int()</code> because it is a number. Then it will convert to integer by removing fractional parts.</p>

python python-3.x floating-point int