{"id":66,"date":"2021-03-11T16:32:19","date_gmt":"2021-03-11T16:32:19","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=66"},"modified":"2021-03-26T12:14:33","modified_gmt":"2021-03-26T12:14:33","slug":"rounding-numbers","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/rounding-numbers\/","title":{"rendered":"Rounding Numbers"},"content":{"rendered":"<h1>round() Function<\/h1>\n<h2>Rounding to the nearest whole number<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\naverage = round(average, 2)\r\n#\r\n<\/pre>\n<h2>Rounding to a number of decimal places<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\ninterest = round(interest, 2)\r\n#\r\n<\/pre>\n<hr \/>\n<h1>Convert Floating Point Numbers to Integers (Higher)<\/h1>\n<h2>round() function<\/h2>\n<pre class=\"brush: python; highlight: [3]; title: ; notranslate\" title=\"\">\r\n# round () function will round to nearest integer\r\nfor fp in &#x5B;12.8, 12.1, -12.1, -12.8]:\r\n    integer = round(fp)   \r\n    print(f&quot;{fp:5} rounds to {integer}&quot;)\r\n<\/pre>\n<pre> 12.8 rounds to 13\n 12.1 rounds to 12\n-12.1 rounds to -12\n-12.8 rounds to -13\n<\/pre>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# .5s will round to nearest even integer    \r\nfor fp in &#x5B;10.5, 11.5, 12.5, 13.5]:\r\n    integer = round(fp)   \r\n    print(f&quot;{fp:5} rounds to {integer}&quot;)\r\n<\/pre>\n<pre> 10.5 rounds to 10\n 11.5 rounds to 12\n 12.5 rounds to 12\n 13.5 rounds to 14<\/pre>\n<h2>int() function<\/h2>\n<pre class=\"brush: python; highlight: [4]; title: ; notranslate\" title=\"\">\r\n# int() function will remove any decimal values \r\n# this is the same as rounding to towards zero \r\nfor fp in &#x5B;12.8, 12.1, -12.1, -12.8]: \r\n    integer = int(fp) \r\n    print(f&quot;The integer part of {fp:5} is {integer}&quot;) \r\n# <\/pre>\n<pre>The integer part of 12.8 is 12\nThe integer part of 12.1 is 12\nThe integer part of -12.1 is -12\nThe integer part of -12.8 is -12<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>round() Function Rounding to the nearest whole number # average = round(average, 2) # Rounding to a number of decimal places # interest = round(interest, 2) # Convert Floating Point Numbers to Integers (Higher) round() function # round () function will round to nearest integer for fp in &#x5B;12.8, 12.1, -12.1, -12.8]: integer = round(fp) print(f&quot;{fp:5} rounds to {integer}&quot;) 12.8 rounds to 13 12.1 rounds to 12 -12.1 rounds to<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/rounding-numbers\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":294,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-66","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/66\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/66\/revisions\/615"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/294"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}