{"id":25119,"date":"2020-03-27T12:15:08","date_gmt":"2020-03-27T10:15:08","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=25119"},"modified":"2020-06-23T14:49:10","modified_gmt":"2020-06-23T11:49:10","slug":"how-to-round-a-number-in-python-truncation-arithmetic-and-more","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/","title":{"rendered":"How to Round a Number in Python: Truncation, Arithmetic, and More"},"content":{"rendered":"\n<p>Remember learning to round in grade school? Me too! The only problem is I don\u2019t use the idea very often. As a result, I don\u2019t always remember how to round a number in programming contexts like Python. Luckily, I\u2019ve pieced together a little article for myself. Hopefully, you get some value out of it as well.<\/p>\n\n\n\n<p>As it turns out, there are a ton of ways to a round a number in Python. For instance, we could truncate the fraction altogether using a cast to int: <code>int()<\/code>. Of course, there are more sophisticated options like the <code>round()<\/code> function which rounds to the nearest even number for midway values like 7.5. That said, feel free to roll your own solution. I built my own \u201cround-half-up\u201d solution using the ternary operator: <code>int(x + .5) if x &gt;= 0 else int(x - .5)<\/code>. Check out the rest of the article for details. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Problem Description<\/h2>\n\n\n\n<p>Rounding is one of those operations we sort of take for granted in everyday life. For instance, I use Acorns which rounds up my purchases to the nearest whole dollar and invests the excess on my behalf.<\/p>\n\n\n\n<p>Unfortunately, rounding to whole numbers isn\u2019t an obvious operation in programming. There\u2019s no operator for rounding in most languages, and I doubt there ever will be. Instead, we often have to lean on a library or roll own one.<\/p>\n\n\n\n<p>To make things more complicated, rounding isn\u2019t always an obvious operation. For example, how do we know when to round up or down? The way I was taught in school was to round numbers up (away from zero) when the decimal is .5 or greater.<\/p>\n\n\n\n<p>As it turns out, there are a lot of different ways to round a whole number. In fact, I found this interesting article in the Electronic Engineering Times which outlines <a href=\"https:\/\/www.eetimes.com\/an-introduction-to-different-rounding-algorithms\/#:~:text=\">several different rounding methods<\/a>. To summarize, here are a few options:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Round-toward-nearest<\/strong>: round to the closest number (but, what about .5?)<\/li><li><strong>Round-half-up<\/strong>: round-toward-nearest where .5 rounds away from zero (e.g 4.5 rounds to 5)<\/li><li><strong>Round-half-down<\/strong>: round-toward-nearest where .5 rounds toward zero (e.g. 4.5 rounds to 4)<\/li><li><strong>Round-half-even<\/strong>: round-toward-nearest where .5 rounds toward the nearest even number (e.g. 4.5 rounds to 4 while 5.5 rounds to 6)<\/li><li><strong>Round-half-odd<\/strong>: round-toward-nearest where .5 rounds toward the nearest odd number (e.g. 4.5 rounds to 5 while 5.5. rounds to 5)<\/li><li><strong>Round-alternate<\/strong>: round-toward-nearest where .5 alternates between rounding up and down over time (e.g. 4.5 rounds to 5 <em>then<\/em> 5.5 rounds to 5)<\/li><li><strong>Round-random<\/strong>: round-toward-nearest where .5 rounds up or down randomly (e.g 4.5 could round to either 4 or 5)<\/li><li><strong>Round-cieling<\/strong>: round all decimals toward positive infinity (e.g 4.3 rounds to 5 while -4.7 rounds to -4)<\/li><li><strong>Round-floor<\/strong>: round all decimals toward negative infinity (e.g. 4.7 rounds to 4 while -4.7 rounds to -5)<\/li><li><strong>Round-toward-zero<\/strong>: round all decimals toward zero (e.g. 4.7 rounds to 4 while -4.7 rounds to -4)<\/li><li><strong>Round-away-from-zero<\/strong>: round all decimals away from zero (e.g. 4.3 rounds to 5 while -4.3 rounds to -5)<\/li><\/ul>\n\n\n\n<p>Clearly, there are a lot of ways to round numbers. For the purposes of this article, we\u2019ll be using the \u201cround-half-up\u201d method. In other words, numbers like 3.5, 5.5, and -2.5 will all round up to 4, 6, and -3, respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solutions<\/h2>\n\n\n\n<p>In this article, we\u2019ll take a look at a few different ways of rounding numbers in Python. As always, we\u2019ll start with the straightforward or brute force solutions. Then, we\u2019ll move our way through more common solutions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rounding by Truncation<\/h3>\n\n\n\n<p>One way to round a number is to trim the decimal place off through truncation:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_11200\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(<\/code><code class=\"java value\">5.3<\/code><code class=\"java plain\">)&nbsp; # stores <\/code><code class=\"java value\">5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>In this example, <code>x<\/code> will store 5 as we trim off the .3. If we were to change the example value to something should round up, we\u2019ll be disappointed:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_860972\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(<\/code><code class=\"java value\">5.7<\/code><code class=\"java plain\">)&nbsp; # stores <\/code><code class=\"java value\">5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Clearly, this isn\u2019t the \u201cround-half-up\u201d solution we discussed above, but it\u2019s a nice shortcut if we just need to remove the decimal place (i.e. \u201cround-toward-zero\u201d).<\/p>\n\n\n\n<p>That said, the simplicity of this solution gives us a nice benefit: truncation works for negative numbers as well:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_276957\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(-<\/code><code class=\"java value\">5.7<\/code><code class=\"java plain\">)&nbsp; # stores -<\/code><code class=\"java value\">5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Of course, if we want a true \u201cround-half-up\u201d solution, we\u2019ll need to try something else.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rounding by Control Flow<\/h3>\n\n\n\n<p>If we think about how \u201cround-half-up\u201d works, then we can probably piece together some if statements to get it done:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_740907\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java value\">5.3<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">fraction = x - <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java keyword\">if<\/code> <code class=\"java plain\">abs(fraction) &gt;= .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">offset = <\/code><code class=\"java value\">1<\/code> <code class=\"java plain\">- fraction<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x = x + offset<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java keyword\">else<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x = x - fraction<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Here, we can compute the fractional portion of a number by using our previous truncation solution. In other words, we can subtract the truncated value from the actual value to get the fraction. In this case, <code>int(x)<\/code> will return 5 which we\u2019ll subtract from 5.3. As a result, <code>fraction<\/code> stores .3 (<a href=\"https:\/\/therenegadecoder.com\/code\/the-remainder-operator-works-on-doubles-in-java\/\">ish<\/a>).<\/p>\n\n\n\n<p>Then, we can use that fraction to perform some control flow. For instance, if the absolute value of <code>fraction<\/code> is greater than or equal to .5, we know we need to round up. Here, the absolute value accounts for the fact that <code>fraction<\/code> could be positive or negative. Otherwise, we might have to write a slightly more annoying if statement. If you want to learn more about computing absolute value in Python, <a href=\"https:\/\/therenegadecoder.com\/code\/how-to-compute-absolute-value-in-python\">I have a whole separate article on that<\/a>.<\/p>\n\n\n\n<p>At any rate, to round a number up, we need to compute the distance to the next number which we call <code>offset<\/code>. We can compute that by subtracting <code>fraction<\/code> from 1. Now, it\u2019s just a matter of adding the offset to <code>x<\/code>, and we\u2019re done.<\/p>\n\n\n\n<p>On the other hand, if we find that the absolute value of <code>fraction<\/code> is actually less than .5, we can subtract that fraction directly from <code>x<\/code>. This will work regardless of if <code>x<\/code> is positive or negative.<\/p>\n\n\n\n<p>If we want to go the extra mile, we could cast <code>x<\/code> to an integer. That said, this should get the job done\u2014barring any pesky rounding errors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rounding by Arithmetic<\/h3>\n\n\n\n<p>Another really clever way to \u201cround-half-up\u201d is to take advantage of the truncation solution from above with a slight modification:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_984978\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(<\/code><code class=\"java value\">5.3<\/code> <code class=\"java plain\">+ .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Here, we\u2019ve added .5 directly to <code>x<\/code>. If the fractional portion of <code>x<\/code> happens to be .5 or greater, <code>x<\/code> will roll over into the next number. Then, when we truncate <code>x<\/code>, we\u2019ll have successfully rounded it.<\/p>\n\n\n\n<p>On the other hand, if the fractional portion of <code>x<\/code> is below .5, the whole number portion of <code>x<\/code> will stay the same. As a result, truncating <code>x<\/code> will have the effect of rounding the number.<\/p>\n\n\n\n<p>Unfortunately, this solution won\u2019t work when <code>x<\/code> is negative. To handle that case, we\u2019ll need some sort of branch. Because I\u2019m lazy, and I like one-liners, I\u2019m going to opt for the ternary:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_510516\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java value\">5.3<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x + .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">) <\/code><code class=\"java keyword\">if<\/code> <code class=\"java plain\">x &gt;= <\/code><code class=\"java value\">0<\/code> <code class=\"java keyword\">else<\/code> <code class=\"java keyword\">int<\/code><code class=\"java plain\">(x - .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Now, if <code>x<\/code> is negative, we\u2019ll subtract .5 rather than adding it. If there\u2019s a more clever solution, let me know in the comments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rounding With the <code>round()<\/code> Function<\/h3>\n\n\n\n<p>If writing a rounding algorithm by hand is out of the question, Python actually provides a built-in rounding function:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_114760\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">round(<\/code><code class=\"java value\">5.3<\/code><code class=\"java plain\">)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Unfortunately, it\u2019s behavior doesn\u2019t quite map out to our \u201cround-half-up\u201d algorithm. Instead, it\u2019s a bit more complicated. Let\u2019s take a look at a few examples:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_658558\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<div class=\"line number16 index15 alt1\">16<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(.<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java value\">0<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(-.<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java value\">0<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(<\/code><code class=\"java value\">1.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java value\">2<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(<\/code><code class=\"java value\">2.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java value\">2<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(<\/code><code class=\"java value\">3.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java value\">4<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(-<\/code><code class=\"java value\">1.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"java plain\">-<\/code><code class=\"java value\">2<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(-<\/code><code class=\"java value\">2.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"java plain\">-<\/code><code class=\"java value\">2<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(-<\/code><code class=\"java value\">3.5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"java plain\">-<\/code><code class=\"java value\">4<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>If we look back at our list of rounding algorithms, we\u2019ll find that the Python developers have actually implemented the \u201cround-half-even\u201d algorithm. When I did some research on this algorithm, I found that it\u2019s sometimes called <a href=\"https:\/\/wiki.c2.com\/?BankersRounding#:~:text=\">bankers rounding<\/a>\u2014the more you know!<\/p>\n\n\n\n<p>Honestly, there\u2019s not much else to say about this solution. However, it\u2019s important to note that the round function in Python can actually work for floating point values as well. For example, we can round out to the tenths place as follows:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_915161\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">&gt;&gt;&gt; round(<\/code><code class=\"java value\">3.52<\/code><code class=\"java plain\">, <\/code><code class=\"java value\">1<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java value\">3.5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>How cool is that?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Performance<\/h2>\n\n\n\n<p>With the solutions out of the way, let\u2019s take a look at how they perform. To do that, we\u2019ll need to capture each solution in a string:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_176808\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<div class=\"line number16 index15 alt1\">16<\/div>\n<div class=\"line number17 index16 alt2\">17<\/div>\n<div class=\"line number18 index17 alt1\">18<\/div>\n<div class=\"line number19 index18 alt2\">19<\/div>\n<div class=\"line number20 index19 alt1\">20<\/div>\n<div class=\"line number21 index20 alt2\">21<\/div>\n<div class=\"line number22 index21 alt1\">22<\/div>\n<div class=\"line number23 index22 alt2\">23<\/div>\n<div class=\"line number24 index23 alt1\">24<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">setup = <\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">x = <\/code><code class=\"java value\">2.5<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number4 index3 alt1\">&nbsp;<\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java plain\">truncation = <\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x)<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number8 index7 alt1\">&nbsp;<\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java plain\">control_flow = <\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java plain\">fraction = x - <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x)<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java keyword\">if<\/code> <code class=\"java plain\">abs(fraction) &gt;= .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">offset = <\/code><code class=\"java value\">1<\/code> <code class=\"java plain\">- fraction<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x + offset<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"java keyword\">else<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x - fraction<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number17 index16 alt2\">&nbsp;<\/div>\n<div class=\"line number18 index17 alt1\"><code class=\"java plain\">arithmetic = <\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number19 index18 alt2\"><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x + .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">) <\/code><code class=\"java keyword\">if<\/code> <code class=\"java plain\">x &gt;= <\/code><code class=\"java value\">0<\/code> <code class=\"java keyword\">else<\/code> <code class=\"java keyword\">int<\/code><code class=\"java plain\">(x - .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number20 index19 alt1\"><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number21 index20 alt2\">&nbsp;<\/div>\n<div class=\"line number22 index21 alt1\"><code class=\"java plain\">banker = <\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number23 index22 alt2\"><code class=\"java plain\">round(x)<\/code><\/div>\n<div class=\"line number24 index23 alt1\"><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>With our strings ready to go, all we need to do is load in the <code>timeit<\/code> library and launch our tests:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_986799\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">&gt;&gt;&gt; <\/code><code class=\"java keyword\">import<\/code> <code class=\"java plain\">timeit<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">&gt;&gt;&gt; min(timeit.repeat(setup=setup, stmt=truncation))<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java value\">0.1537370000005467<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java plain\">&gt;&gt;&gt; min(timeit.repeat(setup=setup, stmt=control_flow))<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java value\">0.43060659999900963<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java plain\">&gt;&gt;&gt; min(timeit.repeat(setup=setup, stmt=arithmetic))<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java value\">0.2925704000008409<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java plain\">&gt;&gt;&gt; min(timeit.repeat(setup=setup, stmt=banker))<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java value\">0.25559939999948256<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>Perhaps unsurprisingly, truncation wins in the speed contest. However, the built-in <code>round()<\/code> function is actually quite quick! I imagine that\u2019s because the function is implemented in a lower level language.<\/p>\n\n\n\n<p>As always, take these measurements with a grain of salt. I ran each of them on a Windows 10 machine with Python 3.7.3. Also, if you\u2019re interested in this performance testing process, <a href=\"https:\/\/therenegadecoder.com\/code\/how-to-performance-test-python-code\/\">I have a whole article about it<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Challenge<\/h2>\n\n\n\n<p>When it comes to rounding, there are a ton of different algorithms. And for each algorithm, there are probably thousands of contexts where they are used. Naturally, I thought it would be fun to make you apply the rounding algorithm in one of those contexts, but I figured it might be more fun to dig into other rounding algorithms instead.<\/p>\n\n\n\n<p>For this challenge, I\u2019m asking you to implement your own bankers rounding algorithm. In other words, write some python code that would implement that same style of rounding that is provided by Python\u2019s <code>round()<\/code> function.<\/p>\n\n\n\n<p>When you think you have something, wrap it in a function and test it on the following inputs:<\/p>\n\n\n\n<figure>\n<table border=\"1\">\n<thead>\n<tr>\n<th>Description<\/th>\n<th>Input<\/th>\n<th>Output<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Near Zero<\/td>\n<td>0.5<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>Standard Case<\/td>\n<td>0.7<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>Standard Case<\/td>\n<td>1.2<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>Even Round Up<\/td>\n<td>1.5<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>Even Round Down<\/td>\n<td>2.5<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>Even Round Up Negative<\/td>\n<td>-1.5<\/td>\n<td>-2<\/td>\n<\/tr>\n<tr>\n<td>Even Round Down Negative<\/td>\n<td>-2.5<\/td>\n<td>-2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>Then, when you\u2019re ready, your solution in the comments!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Little Recap<\/h2>\n\n\n\n<p>At long last, we\u2019ve reached the end of this post. As always, here\u2019s a list of each solution used in this article:<\/p>\n\n\n\n<div>\n<div id=\"highlighter_945372\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<div class=\"line number16 index15 alt1\">16<\/div>\n<div class=\"line number17 index16 alt2\">17<\/div>\n<div class=\"line number18 index17 alt1\">18<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">x = <\/code><code class=\"java value\">17.1<\/code><\/div>\n<div class=\"line number2 index1 alt1\">&nbsp;<\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\"># Truncation<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x)<\/code><\/div>\n<div class=\"line number5 index4 alt2\">&nbsp;<\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java plain\"># Control flow rounding<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java plain\">fraction = x - <\/code><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x)<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java keyword\">if<\/code> <code class=\"java plain\">abs(fraction) &gt;= .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">offset = <\/code><code class=\"java value\">1<\/code> <code class=\"java plain\">- fraction<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x + offset<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java keyword\">else<\/code><code class=\"java plain\">:<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">x - fraction<\/code><\/div>\n<div class=\"line number13 index12 alt2\">&nbsp;<\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"java plain\"># Arithmetic rounding<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"java keyword\">int<\/code><code class=\"java plain\">(x + .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">) <\/code><code class=\"java keyword\">if<\/code> <code class=\"java plain\">x &gt;= <\/code><code class=\"java value\">0<\/code> <code class=\"java keyword\">else<\/code> <code class=\"java keyword\">int<\/code><code class=\"java plain\">(x - .<\/code><code class=\"java value\">5<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number16 index15 alt1\">&nbsp;<\/div>\n<div class=\"line number17 index16 alt2\"><code class=\"java plain\"># Functional rounding<\/code><\/div>\n<div class=\"line number18 index17 alt1\"><code class=\"java plain\">round(x)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n\n\n\n<p>If you got any value out of this article, consider supporting The Renegade Coder by heading over to <a href=\"https:\/\/therenegadecoder.com\/blog\/ways-you-can-help-grow-the-renegade-coder\/\">my list of ways to help grow the site<\/a>. <\/p>\n\n\n\n<p>In addition, you might find value in the following related posts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/therenegadecoder.com\/code\/how-to-compute-absolute-value-in-python\">How to Compute Absolute Value in Python<\/a><\/li><li><a href=\"https:\/\/therenegadecoder.com\/code\/the-remainder-operator-works-on-doubles-in-java\/\">The Remainder Operator Works on Doubles in Java<\/a><\/li><\/ul>\n\n\n\n<p>With all that said, thanks for stopping by. Hope to see you back here soon!<\/p>\n\n\n\n<fieldset><legend>Series Navigation<\/legend>\n<p><a title=\"\u2190 How to Create a List in Python: Loops, Comprehensions, and More\" href=\"https:\/\/therenegadecoder.com\/code\/how-to-create-a-list-in-python\/\">\u2190 How to Create a List in Python: Loops, Comprehensions, and More<\/a><\/p>\n<\/fieldset>\n\n\n\n<p>Advertisements<\/p>\n\n\n\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>Published on Web Code Geeks with permission by Jeremy Grifski, partner at our <a href=\"\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener noreferrer\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Round a Number in Python: Truncation, Arithmetic, and More<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Remember learning to round in grade school? Me too! The only problem is I don\u2019t use the idea very often. As a result, I don\u2019t always remember how to round a number in programming contexts like Python. Luckily, I\u2019ve pieced together a little article for myself. Hopefully, you get some value out of it as &hellip;<\/p>\n","protected":false},"author":14722,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-25119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-27T10:15:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-23T11:49:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jeremy Grifski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@RenegadeCoder94\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeremy Grifski\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/\"},\"author\":{\"name\":\"Jeremy Grifski\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1\"},\"headline\":\"How to Round a Number in Python: Truncation, Arithmetic, and More\",\"datePublished\":\"2020-03-27T10:15:08+00:00\",\"dateModified\":\"2020-06-23T11:49:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/\"},\"wordCount\":1632,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/\",\"url\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/\",\"name\":\"How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2020-03-27T10:15:08+00:00\",\"dateModified\":\"2020-06-23T11:49:10+00:00\",\"description\":\"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Round a Number in Python: Truncation, Arithmetic, and More\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1\",\"name\":\"Jeremy Grifski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g\",\"caption\":\"Jeremy Grifski\"},\"description\":\"Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.\",\"sameAs\":[\"https:\/\/therenegadecoder.com\/\",\"https:\/\/www.linkedin.com\/in\/jeremy-grifski-22240552\/\",\"https:\/\/x.com\/RenegadeCoder94\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/jeremy-grifski\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026","description":"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026","og_description":"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.","og_url":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2020-03-27T10:15:08+00:00","article_modified_time":"2020-06-23T11:49:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Jeremy Grifski","twitter_card":"summary_large_image","twitter_creator":"@RenegadeCoder94","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Jeremy Grifski","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/"},"author":{"name":"Jeremy Grifski","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1"},"headline":"How to Round a Number in Python: Truncation, Arithmetic, and More","datePublished":"2020-03-27T10:15:08+00:00","dateModified":"2020-06-23T11:49:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/"},"wordCount":1632,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/how-to-round-a-number-in-python-truncation-arithmetic-and-more\/","url":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/","name":"How to Round a Number in Python: Truncation, Arithmetic, and More - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage"},"image":{"@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2020-03-27T10:15:08+00:00","dateModified":"2020-06-23T11:49:10+00:00","description":"Interested to learn about Round a Number? Check our article explaining how How to Round a Number in Python with examples.","breadcrumb":{"@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/therenegadecoder.com\/code\/how-to-round-a-number-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"How to Round a Number in Python: Truncation, Arithmetic, and More"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/7eaa8e89cf23a5de82a048beb4c59aa1","name":"Jeremy Grifski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9d50311a27dff0a54d775b6106b8f804d5d11d0b5182ed537f325046a020c6f5?s=96&d=mm&r=g","caption":"Jeremy Grifski"},"description":"Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.","sameAs":["https:\/\/therenegadecoder.com\/","https:\/\/www.linkedin.com\/in\/jeremy-grifski-22240552\/","https:\/\/x.com\/RenegadeCoder94"],"url":"https:\/\/www.webcodegeeks.com\/author\/jeremy-grifski\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/25119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/14722"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=25119"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/25119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=25119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=25119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=25119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}