{"id":37074,"date":"2022-11-28T08:20:28","date_gmt":"2022-11-28T08:20:28","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37074"},"modified":"2023-02-16T19:56:31","modified_gmt":"2023-02-16T19:56:31","slug":"numpy-around","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-around","title":{"rendered":"NumPy around &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Welcome to another tutorial on <a href=\"https:\/\/www.askpython.com\/python\/numpy-trigonometric-functions\" data-type=\"post\" data-id=\"14347\"><strong>NumPy Mathematical Functions<\/strong><\/a>. In this tutorial, we will learn how to use the <code>NumPy around<\/code> function in detail, we will also practice a variety of examples to make our understanding clear.<\/p>\n\n\n\n<p>We all must have solved different types of problems in Mathematics or Physics in which the problem stated that round the final answer to 2 decimal places. Let&#8217;s say we have to round <strong>1.7<\/strong>, we would think that 1.7 is <strong>nearest<\/strong> to 2 so after rounding off 1.7 the value will be 2.<\/p>\n\n\n\n<p>We can do this using programming as well which we will be learning through this tutorial. So, without any further due let&#8217;s start.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp\" data-type=\"post\" data-id=\"37466\">NumPy exp \u2013 A Complete Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy around?<\/h2>\n\n\n\n<p>NumPy around is one of the mathematical functions of the NumPy library which <strong>rounds the number<\/strong> passed as input to the function. <\/p>\n\n\n\n<p>Let&#8217;s look at the syntax of the NumPy around function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy around<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.around(a, decimals=0, out=None)\n<\/pre><\/div>\n\n\n<p>Let&#8217;s understand the parameters of this function.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>a<\/strong><\/code> &#8211; is the input number which needs to be rounded off. It can be a <strong>single number<\/strong> as well as a<strong> NumPy array of numbers<\/strong>.<\/li>\n\n\n\n<li><strong><code>decimals<\/code><\/strong> &#8211; The value of the decimals is always an <strong>integer<\/strong>. It specifies the number of decimal places up to which we want to round the input number. It&#8217;s an optional parameter. Its default value is 0.<\/li>\n\n\n\n<li><strong><code>out<\/code><\/strong> &#8211; It is the output of the numpy.around() function. It&#8217;s also an optional parameter.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Working with the NumPy around<\/h2>\n\n\n\n<p>Let&#8217;s now write some code to understand this function better.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy around with a single number as input<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Rounding off some integer values\nprint(&quot;Around of 1 is:&quot;,np.around(1))\n\nprint(&quot;Around of 5 is:&quot;,np.around(5))\n\n# Rounding off some decimal values\nprint(&quot;Around of 5.5 is:&quot;,np.around(5.5))\n\nprint(&quot;Around of 9.54 is:&quot;,np.around(9.54))\n\nprint(&quot;Around of 12.70 is:&quot;,np.around(12.70))\n\nprint(&quot;Around of 9.112 is:&quot;,np.around(9.112))\n\nprint(&quot;Around of 10.112 is:&quot;,np.around(10.112))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nAround of 1 is: 1\nAround of 5 is: 5\nAround of 5.5 is: 6.0\nAround of 9.54 is: 10.0\nAround of 12.70 is: 13.0\nAround of 9.112 is: 9.0\nAround of 10.112 is: 10.0\n<\/pre><\/div>\n\n\n<p>In the above output, the integer numbers remain the same even after rounding off. Every other output is rounded off to a number with a precision of 1 digit after the decimal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy around with decimals parameter<\/h3>\n\n\n\n<p>The <strong><code>decimals<\/code><\/strong> argument allows us to specify the number of decimal places to which we want to round the input number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Round to ones place\nprint(&quot;Around 5.145 is:&quot;,np.around(5.145 , 0))\n\n# Round to tenths place\nprint(&quot;Around 5.145 is:&quot;,np.around(5.145 , 1))\n\n# Round to hundredths place\nprint(&quot;Around 5.145 is:&quot;,np.around(5.145 , 2))\n\n# Round to thousandths place\nprint(&quot;Around 5.145 is:&quot;,np.around(5.145 , 3))\n\n# Returns the same number\nprint(&quot;Around 5.145 is:&quot;,np.around(5.145 , 10))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nAround 5.145 is: 5.0\nAround 5.145 is: 5.1\nAround 5.145 is: 5.14\nAround 5.145 is: 5.145\nAround 5.145 is: 5.145\n<\/pre><\/div>\n\n\n<p>The above output is pretty much clear where the digits are rounded up to the specified value of the <strong><code>decimals<\/code><\/strong>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy around with the negative value of decimals<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Round to tenths place\nprint(&quot;Around 455.56 is:&quot;,np.around(455.56 , -1))\n\n# Round to hundredths place\nprint(&quot;Around 455.56 is:&quot;,np.around(455.56 , -2))\n\n# Round to thousandths place\nprint(&quot;Around 455.56 is:&quot;,np.around(455.56 , -3))\n\n# Round to tenths place    \nprint(&quot;Around 455 is:&quot;,np.around(455 , -1))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nAround 455.56 is: 460.0\nAround 455.56 is: 500.0\nAround 455.56 is: 0.0\nAround 455 is: 460\n<\/pre><\/div>\n\n\n<p>If the value of the <strong><code>decimals<\/code><\/strong> is set to some negative value then the <strong>non-decimal<\/strong> digits of the input number are rounded.<\/p>\n\n\n\n<p>Let&#8217;s understand rounding of 455.56 with decimals value set to -2. Here, -2 implies the hundredth place in the number. Now, count the hundredth place to the left of the decimal in the number, and accordingly, the number is rounded.<\/p>\n\n\n\n<p><strong>Note:<\/strong> Rounding beyond the leftmost digit of the input number will result in 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy around with NumPy Array <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.array((1 , 3 , 5 , 100 , 145 , 344 , 745))\n\nprint(&quot;\\n&quot;)\nprint(&quot;Input Array:\\n&quot;,a)\nprint(&quot;Result :\\n&quot;,np.around(a))\n\nprint(&quot;\\n&quot;)\nprint(&quot;Input Array:\\n&quot;,a)\nprint(&quot;Rounded Values:\\n&quot;,np.around(a , -1))\n\nb = np.array((0.5 , 1.5 , 1.7 , 3.5 , 7.5 , 9.8))\n\nprint(&quot;\\n&quot;)\nprint(&quot;Input Array:\\n&quot;,b)\nprint(&quot;Rounded Values:\\n&quot;,np.around(b))\n\nc = np.array((4.567 , 13.564 , 12.334 , 1.567 , 9.485 , 4.444))\n\nprint(&quot;\\n&quot;)\nprint(&quot;Input Array:\\n&quot;,c)\nprint(&quot;Rounded Values:\\n&quot;,np.around(c , 2))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nInput Array:\n &#x5B;  1   3   5 100 145 344 745]\nResult :\n &#x5B;  1   3   5 100 145 344 745]\n\n\nInput Array:\n &#x5B;  1   3   5 100 145 344 745]\nRounded Values:\n &#x5B;  0   0   0 100 140 340 740]\n\n\nInput Array:\n &#x5B;0.5 1.5 1.7 3.5 7.5 9.8]\nRounded Values:\n &#x5B; 0.  2.  2.  4.  8. 10.]\n\n\nInput Array:\n &#x5B; 4.567 13.564 12.334  1.567  9.485  4.444]\nRounded Values:\n &#x5B; 4.57 13.56 12.33  1.57  9.48  4.44]\n<\/pre><\/div>\n\n\n<p>There is one interesting thing to note that <code><strong>np.around(<\/strong><\/code><strong><code>)<\/code><\/strong> returns a <strong>new ndarray<\/strong> after rounding and doesn&#8217;t affect the original ndarray.<\/p>\n\n\n\n<p>This is where the third parameter comes into play. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using NumPy around with the parameter out<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.array((1.34 , 2.56 , 3.99 , 45.45 , 100.01))\n\nprint(&quot;Original Array:\\n&quot;,a)\n\nnp.around(a , out=a)\n\nprint(&quot;Array after Rounding the values:\\n&quot;,a)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nOriginal Array:\n &#x5B;  1.34   2.56   3.99  45.45 100.01]\nArray after Rounding the values:\n &#x5B;  1.   3.   4.  45. 100.]\n<\/pre><\/div>\n\n\n<p>In the above program, the rounded values are stored in the original array. <\/p>\n\n\n\n<p>That was all from my side, do execute the code discussed in the article and try using the function with different inputs as well. Do check out the official documentation of the NumPy around function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.around.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Documentation &#8211; NumPy around<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to another tutorial on NumPy Mathematical Functions. In this tutorial, we will learn how to use the NumPy around function in detail, we will also practice a variety of examples to make our understanding clear. We all must have solved different types of problems in Mathematics or Physics in which the problem stated that [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":37465,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37074","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\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37074"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37074\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37465"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}