{"id":2245,"date":"2021-02-20T04:21:14","date_gmt":"2021-02-20T04:21:14","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2245"},"modified":"2021-02-20T04:42:13","modified_gmt":"2021-02-20T04:42:13","slug":"python-float-to-int","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-float-to-int\/","title":{"rendered":"Python Float to Int"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to convert a float to an integer.<\/p>\n\n\n\n<p>Suppose that you have a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\">float<\/a> such as 20.3, and you want to convert it to an <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\">integer<\/a>.<\/p>\n\n\n\n<p>When you convert a float to an integer, you&#8217;ll have a data loss. For example, <code>20.3<\/code> may become <code>20<\/code> or <code>21<\/code>.<\/p>\n\n\n\n<p>Python provides you with some functions in the <code>math<\/code> module for converting from a <code>float<\/code> to an <code>int<\/code>, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Truncation<\/li><li>Floor<\/li><li>ceiling<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='truncation'>Truncation <a href=\"#truncation\" class=\"anchor\" id=\"truncation\" title=\"Anchor for Truncation\">#<\/a><\/h2>\n\n\n\n<p>The <code>trunc(x)<\/code> function returns the integer part of the number <code>x<\/code>. It ignores everything after the decimal point. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> trunc\n\nprint(trunc(<span class=\"hljs-number\">12.2<\/span>))\nprint(trunc(<span class=\"hljs-number\">12.5<\/span>))\nprint(trunc(<span class=\"hljs-number\">12.7<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">12<\/span>\n<span class=\"hljs-number\">12<\/span>\n<span class=\"hljs-number\">12<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Similarly, the int() constructor accepts a float and uses truncation to cast a <code>float<\/code> to an <code>int<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(int(<span class=\"hljs-number\">12.2<\/span>))\nprint(int(<span class=\"hljs-number\">12.5<\/span>))\nprint(int(<span class=\"hljs-number\">12.7<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='floor'>Floor <a href=\"#floor\" class=\"anchor\" id=\"floor\" title=\"Anchor for Floor\">#<\/a><\/h2>\n\n\n\n<p>The <code>floor(x)<\/code> function returns the largest integer less than or equal to <code>x<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> floor\n\nprint(floor(<span class=\"hljs-number\">12.2<\/span>))\nprint(floor(<span class=\"hljs-number\">12.5<\/span>))\nprint(floor(<span class=\"hljs-number\">12.7<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">12<\/span>\n<span class=\"hljs-number\">12<\/span>\n<span class=\"hljs-number\">12<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows how floor() function is applied to a positive number:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"133\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/02\/Python-float-to-int-floor-a-positive-number.png\" alt=\"\" class=\"wp-image-2247\"\/><\/figure><\/div>\n\n\n\n<p>For positive numbers, <code>floor(x)<\/code> and <code>trunc(x)<\/code> return the same result. However, it&#8217;s not the case for negative numbers. For example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"136\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/02\/Python-float-to-int-floor-a-negative-number.png\" alt=\"\" class=\"wp-image-2248\"\/><\/figure><\/div>\n\n\n\n<p>The following picture shows how the <code>floor()<\/code> function is applied to a negative number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> floor, trunc\n\nprint(floor(<span class=\"hljs-number\">-12.7<\/span>))\nprint(trunc(<span class=\"hljs-number\">-12.7<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">-13<\/span>\n<span class=\"hljs-number\">-12<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture illustrates the difference between the <code>floor()<\/code> and <code>trunc()<\/code> function when you apply them to a negative number:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"134\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/02\/Python-float-to-int-floor-vs-trunc.png\" alt=\"Python float to int - floor vs trunc\" class=\"wp-image-2254\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id='ceiling'>Ceiling <a href=\"#ceiling\" class=\"anchor\" id=\"ceiling\" title=\"Anchor for Ceiling\">#<\/a><\/h2>\n\n\n\n<p>The <code>ceil(x)<\/code> function returns the smallest integer greater than or equal to <code>x<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> ceil\n\n\nprint(ceil(<span class=\"hljs-number\">12.7<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">13<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following illustrates how the <code>ceil()<\/code> function is applied to a positive number:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"136\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/02\/Python-float-to-int-ceil-a-positive-number.png\" alt=\"\" class=\"wp-image-2249\"\/><\/figure><\/div>\n\n\n\n<p>This example uses the ceil() function for negative numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> ceil\n\nprint(ceil(<span class=\"hljs-number\">-12.7<\/span>))\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">-12<\/code><\/span><\/pre>\n\n\n<p>The following illustrates how the <code>ceil()<\/code> function is applied to a negative number:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"136\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/02\/Python-float-to-int-ceil-a-negative-number.png\" alt=\"\" class=\"wp-image-2250\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Convert a float to an int always results in a data loss.<\/li><li>The <code>trunc()<\/code> function returns the integer part of a number.<\/li><li>The <code>floor()<\/code> function returns the largest integer less than or equal to a number.<\/li><li>The <code>ceil()<\/code> function returns the smallest integer greater than or equal to a number.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2245\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float-to-int\/\"\n\t\t\t\tdata-post-title=\"Python Float to Int\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2245\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float-to-int\/\"\n\t\t\t\tdata-post-title=\"Python Float to Int\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to convert a float to an integer.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2245","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2245"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2245\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}