# Rounding a number to a specific number of decimal places

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 decimal place (.456 rounds up to .46).

MIT App Inventor does not have a function to do this sort of round off – well, not exactly. Actually, it has a formatting function that can accomplish the same thing but it is intended for converting numeric values to text strings.

I created a simple little program to demonstrate how this works, plus a couple of other methods to implement this feature.

Update: This post has been updated with thanks to Taifun for noting that App Inventor does have a “raise to a power function”. See below for more details and a link to his great web site!

## User Interface View

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. Here you can see that 123.4567 has been rounded to 123.457.

## Designer View  ## Blocks View

Seriously – there is not much to this example! If our input value (txtNumber.Text) contains “123.4567”, and the number of decimal places desired (txtDecimalPlaces.Text) is 3, then this says to format “123.4567” with “3” decimal places, producing “123.457”.

This is so simple it hardly needs a tutorial but using the format as decimal block to accomplish this task might not be obvious.

But we can make this a bit more complicated – of course!

## Two More Ways to Implement This Feature

Another way to truncate a number to a specific number of decimal places uses some multiplication and division. Let us say we 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 12345.67, and use the builtin Math “round” function to truncate that to 12346 (it rounds up). Then, divide that value by 100 to turn it back into 123.46.

Suppose you want to set the number of decimal places to some value other than 2? Easy. If dp is a variable holding the number of decimal places, and n is the number to truncate, then our general solution is to implement this equation:

result = round(n *  (10^dp) / 10^dp)

where n is the number we wish to truncate to a number of decimal places, dp is the number of desired decimal places and “^” means raised to a power of dp. If dp is 2, then 10^2 is 100. That is the same as 10 times 10. If we wanted 3 digits, then we calculate 10^3, or 10 x 10 x 10 or 1000.

When trying to understand a math equation, it sometimes helps to plug in numbers so let us do that.

Set n = 456.1234

Set dp = 2 (number of decimal places)

10^dp is 100

This becomes:

456.1234 x 100 or 45612.34

Round 45612.34 to 45612 and then divide by 100, turning this back in to 456.12.

## How do we calculate 10^dp?

Important Update: MIT App Inventor does have a POWER function! It’s one of the arithmetic blocks in the Math components: To calculate 10^x power, substitute 10 at left and the value of X at right. Thanks to Taifun for the correction! Be sure to visit his web site for tons more stuff on App Inventor!

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 dp were 3, then calculate 10 x 10 x 10 or 1000.

We can do this with a simple for each block: If the decimal places is 3, then this repeatedly calculates PowerOfTen x 10, three times, producing a value of 1000.

The value of PowerOfTen is then used in the calculation, rounded off, and finally divided by PowerOfTen.

The second way to do this calculation is to implement our own 10^x power function to calculate values such as 10^2 (or 100) or 10^3 (or 1000) directly without using a for each block.

As mentioned above, MIT App Inventor does not implement a base 10 power function. However, App Inventor does have functions to calculate e^x and the natural logarithm (the inverse of e^x). If you have not yet studied logarithms in school, then this section may not make much sense to you.

Note – MIT App Inventor implements e^x using a feature hidden in the Math blocks; App Inventor uses “log” to refer to the natural logarithm. Some programming languages distinguish between the natural logarithm and the base 10 logarithm, by using the notation ln for the natural logarithm and log for the base 10 logarithm. App Inventor, however, uses log to mean the natural logarithm.

We can calculate 10^x power by instead calculating e^(x log10). This means taking the value of e and raising it the power of x (the number of decimal places) times the natural logarithm of 10.

The blocks code to round a value to specific number of decimal places, using a power function, looks like this: When you look in the Math blocks you will notice there is no e^ or log 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 e^ and log from that list. In this example, I dragged the square root function and changed it to select e^: UPDATED VERSION:

Click on this to see a full size version – this uses the “raise to a power” Math block: You probably just learned way more about rounding off a number to a specific set of decimal places than you ever cared to know!

## 13 thoughts on “Rounding a number to a specific number of decimal places”

1. Edward M says:

Even staring at that list, I missed it! Yes, it does exist Taifun. I will update the original post – thank you for pointing that out!

Like

This site uses Akismet to reduce spam. Learn how your comment data is processed.