{"id":1325,"date":"2016-06-09T17:25:55","date_gmt":"2016-06-10T00:25:55","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=1325"},"modified":"2016-06-09T17:25:55","modified_gmt":"2016-06-10T00:25:55","slug":"rounding-a-number-to-a-specific-number-of-decimal-places","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2016\/06\/09\/rounding-a-number-to-a-specific-number-of-decimal-places\/","title":{"rendered":"Rounding a number to a specific number of decimal places"},"content":{"rendered":"<p>I was working on a small app that needed to round some numeric values. For example, given a number such as 123.456789, I wanted to round this off to two decimal places such as 123.46. Why .46 instead of .45? Because the value is rounded at the <em>n<\/em>-th\u00a0decimal place (.456 rounds up to .46).<\/p>\n<p>MIT App Inventor does not have a function to do this sort of round off &#8211; well, not exactly. Actually, it has a formatting function that can accomplish the same thing but\u00a0it is intended for converting numeric values to text strings.<\/p>\n<p>I created a simple little program to demonstrate\u00a0how this works, plus a couple of other methods to implement this feature.<\/p>\n<p><em><strong>Update: This post has been updated with thanks to Taifun for noting that App Inventor does have a &#8220;raise to a power function&#8221;. See below for more details and a link to his great web site!<\/strong><\/em><\/p>\n<h2>User Interface View<\/h2>\n<p>The user interface prompts for a number, and the number of decimal places to which it should be rounded. Press the Round off number button to see the result.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffUI.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1326 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffUI.jpg\" alt=\"RoundOffUI\" width=\"270\" height=\"185\" \/><\/a>Here you can see that 123.4567 has been rounded to 123.457.<\/p>\n<p><!--more--><\/p>\n<h2>Designer View<\/h2>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffDesignerView.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1327 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffDesignerView.gif\" alt=\"RoundOffDesignerView\" width=\"311\" height=\"181\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffComponents.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1328 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/RoundOffComponents.gif\" alt=\"RoundOffComponents\" width=\"180\" height=\"291\" \/><\/a><\/p>\n<h2>Blocks View<\/h2>\n<p>Seriously &#8211; there is not much to this example!<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks3.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1331 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks3-1024x157.gif\" alt=\"blocks3\" width=\"625\" height=\"96\" \/><\/a><\/p>\n<p>If our input value (<em>txtNumber.Text<\/em>) contains &#8220;123.4567&#8221;, and the number of decimal places desired (<em>txtDecimalPlaces.Text<\/em>) is 3, then this says to format &#8220;123.4567&#8221; with &#8220;3&#8221; decimal places, producing &#8220;123.457&#8221;.<\/p>\n<p>This is so simple it hardly needs a tutorial but using the format as decimal block to accomplish this task might not be obvious.<\/p>\n<p>But we can make this a bit more complicated &#8211; of course!<\/p>\n<h2>Two More Ways to Implement This Feature<\/h2>\n<p>Another way to\u00a0truncate\u00a0a number to a specific number of decimal places uses some multiplication and division. Let us say\u00a0we have 123.4567 and we want to round this off to 2 decimal places. We can multiply this value by 100, as in 123.4567 x 100 and get\u00a012345.67, and use the builtin Math &#8220;round&#8221; function to truncate that to 12346 (it rounds up). Then, divide that value by 100 to turn it back into 123.46.<\/p>\n<p>Suppose you want to set the number of decimal places to some value other than 2? Easy. If <em>dp<\/em> is a variable holding the number of decimal places, and <em>n<\/em> is the number to truncate, then our general solution is to implement this equation:<\/p>\n<p><em>result = round(n *\u00a0 (10^dp) \/ 10^dp)<\/em><\/p>\n<p>where <em>n<\/em> is the number we wish to truncate to a number of decimal places, <em>dp<\/em> is the number of desired decimal places and\u00a0&#8220;^&#8221; means raised to a power of <em>dp<\/em>. If <em>dp<\/em> is 2, then 10^2 is 100. That is the same as\u00a010 times 10. If we wanted 3 digits, then we calculate 10^3,\u00a0or\u00a010 x 10 x 10 or 1000.<\/p>\n<p>When trying to understand\u00a0a math equation, it sometimes helps to plug in numbers so let us do that.<\/p>\n<p>Set <em>n<\/em> = 456.1234<\/p>\n<p>Set <em>dp<\/em> = 2 (number of decimal places)<\/p>\n<p>10^<em>dp<\/em> is 100<\/p>\n<p>This becomes:<\/p>\n<p>456.1234 x 100 or 45612.34<\/p>\n<p>Round 45612.34 to 45612 and then divide by 100, turning this back in to 456.12.<\/p>\n<h2>How do we calculate <em>10^dp<\/em>?<\/h2>\n<p><span style=\"text-decoration: underline;\"><strong>Important Update:<\/strong><\/span> MIT App Inventor <em>does have a POWER function!<\/em> It&#8217;s one of the arithmetic blocks in the Math components:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/Capture.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1338 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/Capture.jpg\" alt=\"Capture\" width=\"147\" height=\"69\" \/><\/a><\/p>\n<p>To calculate 10^x power, substitute 10 at left and the value of X at right. <strong><em>Thanks to <a href=\"https:\/\/puravidaapps.com\/\">Taifun <\/a>for the correction! Be sure to visit his web site for tons more stuff on App Inventor!<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Most programming languages have a built-in power function but MIT App Inventor does not. There are two ways to calculate 10 raised to a power. The first method is to multiply 10 several times. If <em>dp<\/em> were 3, then calculate 10 x 10 x 10 or 1000.<\/p>\n<p>We can do this with a simple <em>for each<\/em> block:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks3-1.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1334 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks3-1-1024x234.gif\" alt=\"blocks3\" width=\"625\" height=\"143\" \/><\/a><\/p>\n<p>If the decimal places is 3, then this repeatedly calculates <em>PowerOfTen<\/em> x 10, three times, producing a value of 1000.<\/p>\n<p>The value of <em>PowerOfTen<\/em> is then used in the calculation, rounded off, and finally\u00a0divided by <em>PowerOfTen<\/em>.<\/p>\n<p>The second way to do this calculation is to implement our own <em>10^x<\/em> power function to calculate values such as 10^2 (or 100) or 10^3 (or 1000) directly without using a <em>for each<\/em> block.<\/p>\n<p>As mentioned above, MIT App Inventor does not implement a base 10 power function. However, App Inventor does have functions to calculate <em>e^x<\/em> and the natural logarithm (the inverse of <em>e^x<\/em>). If you have not yet studied logarithms in school, then this section may not make much sense to you.<\/p>\n<blockquote><p>Note &#8211; MIT App Inventor implements\u00a0<em>e^x<\/em> using a feature hidden in the Math blocks; App Inventor uses &#8220;<em>log<\/em>&#8221; to refer to the natural logarithm. Some programming languages distinguish between the natural logarithm and the base 10 logarithm, by using the notation <em>ln<\/em> for the natural logarithm and <em>log<\/em> for the base 10 logarithm. App Inventor, however, uses <em>log<\/em> to mean the natural logarithm.<\/p><\/blockquote>\n<p>We can calculate <em>10^x<\/em> power by instead calculating <em>e^(x log10)<\/em>. This means taking the value of <em>e<\/em> and raising it the power of <em>x<\/em> (the number of decimal places) times the natural logarithm of 10.<\/p>\n<p>The blocks code to round a value to specific number of decimal places, using a power function, looks like this:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks5.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1335 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks5-1024x142.gif\" alt=\"blocks5\" width=\"625\" height=\"87\" \/><\/a><\/p>\n<p>When you look in the Math blocks you will notice there is no <em>e^<\/em> or <em>log<\/em> math functions! They are there but hidden. Drag a math block to the editor and click on the selected function to pop up a list of all math functions. Select <em>e^<\/em> and <em>log<\/em> from that list. In this example, I dragged the <em>square root<\/em> function and changed it to select <em>e^<\/em>:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks6.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1336 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/blocks6.gif\" alt=\"blocks6\" width=\"178\" height=\"195\" \/><\/a><\/p>\n<p><strong>UPDATED VERSION:<\/strong><\/p>\n<p>Click on this to see a full size version &#8211; this uses the &#8220;raise to a power&#8221; Math block:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/Capture.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1340\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/06\/Capture.gif\" alt=\"Capture\" width=\"988\" height=\"67\" \/><\/a><\/p>\n<p>You\u00a0probably just learned way more about rounding off a number to a specific set of decimal places than you ever cared to know!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a small app that needed to round some numeric values. For example, given a number such as 123.456789, I wanted to round this off to two decimal places such as 123.46. Why .46 instead of .45? Because the value is rounded at the n-th\u00a0decimal place (.456 rounds up to .46). MIT &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2016\/06\/09\/rounding-a-number-to-a-specific-number-of-decimal-places\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Rounding a number to a specific number of decimal places<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1325","post","type-post","status-publish","format-standard","hentry","category-programming-method"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=1325"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1325\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=1325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=1325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=1325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}