{"id":14069,"date":"2021-03-31T17:04:26","date_gmt":"2021-03-31T17:04:26","guid":{"rendered":"https:\/\/www.askpython.com\/?p=14069"},"modified":"2021-03-31T17:04:29","modified_gmt":"2021-03-31T17:04:29","slug":"handle-precision-values","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/handle-precision-values","title":{"rendered":"5 Ways to handle precision values in Python"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will be focusing on <strong>5 Ways to handle precision values<\/strong> in Python. So, let us get started! <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-1\"><\/span><span id=\"eh-1\"><\/span>What do we mean by &#8220;handle precision values&#8221;?<\/h2>\n\n\n\n<p>Be it any programming language, we often come across situations, where we process integer or numeric data into any application or manipulation steps. In the same process, we find data with decimal values. This is when we need to handle precision values.<\/p>\n\n\n\n<p>Python offers us various functions to handle precision values for integer data. It gives us a choice to exclude the decimal points or to have customized values depending upon the position of the decimal values in the number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to handle precision values in Python?<\/h2>\n\n\n\n<p>Let&#8217;s look at the different functions that Python offers for handling the precision data and decimals in our code outputs.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Python % operator<\/strong><\/li><li><strong>Python format() function<\/strong><\/li><li><strong>Python round() function<\/strong><\/li><li><strong>The trunc() function<\/strong><\/li><li><strong>Math ceil() &amp; floor() functions<\/strong><\/li><\/ol>\n\n\n\n<p>Let us now go ahead and have a look at each one of them in the upcoming section.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-2\"><\/span><span id=\"eh-2\"><\/span>1. Python % operator<\/h3>\n\n\n\n<p>With the <a href=\"https:\/\/www.askpython.com\/python\/python-modulo-operator-math-fmod\" class=\"rank-math-link\">&#8216;%&#8217; operator<\/a>, we can format the number as well as set precision limits to the same. By this, we can customize the limits of the precision points to be included in the resultant number.<\/p>\n\n\n\n<p>Have a look at the below syntax!<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#039;%.point&#039;%number\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>point: It refers to the number of points we wish to have after the decimal in the number.<\/li><li>number: The integer value to be worked on.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-3\"><\/span><span id=\"eh-3\"><\/span>2. Python format() function<\/h3>\n\n\n\n<p>Like the % operator, we can make use of the <a href=\"https:\/\/www.askpython.com\/python\/string\/python-format-function\" class=\"rank-math-link\">format() function<\/a> too, which helps us set limits for the precision values. With the format() function, we format the data considering it a string and also set the limits for the points to be included after the decimal portion of the number.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint (&quot;{0:.pointf}&quot;.format(number)) \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-4\"><\/span><span id=\"eh-4\"><\/span>3. Python round() function<\/h3>\n\n\n\n<p>With Python round() function, we can extract and display the integer values in a customized format That is, we can select the number of digits to be displayed after the decimal point as a check for precision handling.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nround(number, point)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-5\"><\/span><span id=\"eh-5\"><\/span>Implementing Precision handling in Python<\/h2>\n\n\n\n<p>In the below example, we have utilized the above explained 3 functions to handle precision values in Python. We are here trying to handle precision by setting the digit to be displayed after the decimal point as 4.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnum = 12.23456801\n\n# using &quot;%&quot; operator\nprint (&#039;%.4f&#039;%num) \n\n# using format() function\nprint (&quot;{0:.4f}&quot;.format(num)) \n\n# using round() function\nprint (round(num,4)) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n12.2346\n12.2346\n12.2346\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-6\"><\/span><span id=\"eh-6\"><\/span>Python math functions to handle precision<\/h2>\n\n\n\n<p>Apart from the above functions, python also offers us with <a href=\"https:\/\/www.askpython.com\/python-modules\/python-math-module\" class=\"rank-math-link\">math module<\/a> that contains a set of functions to deal with the precision values.<\/p>\n\n\n\n<p>Python math module has the below set of functions to deal with precision values&#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The trunc() function<\/li><li>Python ceil() and floor() functions<\/li><\/ol>\n\n\n\n<p>Let us have a look at them one by one.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-7\"><\/span><span id=\"eh-7\"><\/span>4. Python trunc() function<\/h3>\n\n\n\n<p>With trunc() function, all the digits after the decimal points gets terminated. That is, it returns the number preceding the decimal point only.<\/p>\n\n\n\n<p>Have a look at the below syntax.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport math\nmath.trunc(number)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport math\nnum = 12.23456801\n\n# using trunc() function\nprint (math.trunc(num)) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n12\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Python ceil() and floor() function<\/h3>\n\n\n\n<p>With ceil() and floor() function, we can round off the decimal number to the closest high or low value.<\/p>\n\n\n\n<p>The ceil() function rounds off the decimal number to the nearest large value after it. On the other hand, floor() function rounds off the value to the nearest low value before it.<\/p>\n\n\n\n<p><strong>Example: <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport math\nnum = 12.23456801\n\n# using ceil() function\nprint (math.ceil(num)) \n\n# using floor() function\nprint (math.floor(num)) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n13\n12\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, stay tuned with us.<\/p>\n\n\n\n<p>Till then, Happy Learning!! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on 5 Ways to handle precision values in Python. So, let us get started! What do we mean by &#8220;handle precision values&#8221;? Be it any programming language, we often come across situations, where we process integer or numeric data into any application or manipulation steps. In [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14460,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-14069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=14069"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14069\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14460"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=14069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=14069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=14069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}