{"id":2730,"date":"2021-02-11T03:15:13","date_gmt":"2021-02-11T03:15:13","guid":{"rendered":"https:\/\/www.afternerd.com\/blog\/?p=2730"},"modified":"2021-02-11T03:15:59","modified_gmt":"2021-02-11T03:15:59","slug":"round-number-nearest-integer","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/round-number-nearest-integer\/","title":{"rendered":"Python: Round a Number to the Nearest Integer"},"content":{"rendered":"\n<p>In many situations, you will find yourself in a situation where you want to round a floating-point number to the nearest integer in your Python 3 code. In this article, I will explain how.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Using the Round() Function<\/h2>\n\n\n\n<p>You can use the Round built-in function in Python to round a number to the nearest integer.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> round(2.4)\n2\n>>> round(2.6)\n3\n>>> round(2.5)\n2<\/code><\/pre>\n\n\n\n<p>Note that in Python 3, the return type is <strong>int<\/strong>. However, if you are still on Python 2, the return type will be a <strong>float<\/strong> so you would need to cast the returned value into <strong>int<\/strong>.<\/p>\n\n\n\n<p>Another thing to notice here is that according to <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#round\" target=\"_blank\" rel=\"noreferrer noopener\">Python&#8217;s documentation<\/a>, any floating number that is mid-way between two integers (e.g. 2.5, 3.4. etc&#8230;) will be rounded to the <strong>nearest even<\/strong> choice. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>>>> round(2.5)\n2\n>>> round(3.5)\n4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Without Using any Built-in Functions<\/h2>\n\n\n\n<p>If you want to round a number to the nearest integer without using any built-in functions in Python (maybe for a coding interview or something), then you can define the following function.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>def round_number(x):\n    if (x - int(x)) &lt; 0.5:\n        return int(x)\n    else:\n        return int(x) + 1<\/code><\/pre>\n\n\n\n<p>There you go! Happy Python coding \ud83d\ude42<\/p>\n\n\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":2733,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[1091,1787,248,316,160,716,1776,480,1316,1783,158,150,466,1784,1782,1786,342],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2730"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=2730"}],"version-history":[{"count":3,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2730\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/2730\/revisions\/2734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/2733"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=2730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=2730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=2730"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}