Significant figures/ significant digits

Hello everyone! Does MIT App Inventor have a function to do numbers to two significant figures? I can round to the nearest whole number and I can do to two decimal places, but ideally I'd like my output in two significant figures. Thanks in advance!

So you would like to show the value 4321.1234 as 4300 ?

or 0.07039 as 0.070


(I have not tried values < 1 yet)

My code does not handle < 1:

What range of values do you need to cover?

What about with decimal places e.g. 23.7890 ? (which should come out as 24)

Or did you really want scientific notation?


I don't remember where I got this.

There is a comment that it will fail on 0 input, so it needs work.

yes that's it

Ideally it needs to cover anything from 0.01 to 10,000+. I should be able to use an IF statement to sort if the value is <= 1 if needed.
How to I get this function?

math.aia (50.5 KB)
or you can drag the functions from my prior post directly into your blocks editor window.

If you mean the Do It, it is a feature of the Companion.

I'm trying to do the sig figs for each of these calculations. Is there a way to do this, or should I arrange this in a different way?

That's why I coded the calculations as a value function.

Code half a dozen calls to that function and insert them in stream just before the labels.

like this? Do I need to alter the call log10? Sorry, very new at this!

It is not pretty but this seems to work. Works with positive numbers, but easily modified to handle negative numbers. If nothing else, it demonstrates and extols the ability of AI2 to treat numbers as strings and strings as numbers. Returns integers from 0 - 99 as entered. The blocks are draggable when viewed full size

sigNum.aia (6.4 KB)

You need to copy in the log10 value procedure too because I call it from my procedure.

It's in my prior post or in my math.aia

AI2 lacks its own log10 block.

I have previously done this using a set of If-else statements, but have had to input one per calculation (there are about 80 calculations in the app) so this has slowed down the app so much it is almost unusable. Is there any way to covert this set of if-else statements into a procedure that would work for each calculation? Thanks!

Have you read
from the free online book


Read it all the way through, to see how to make and call procedures.

Also, you don't need to test for both ends of each range in your if/then/elseif ladder, if you test in strict ascending or descending order.

You could test
if x > the biggest boundary then ...
else if x > the next biggest boundary then ...
elseif x > the next boundary down then ...

This works because the elseif triggers the first matching range, and none others.

You would save half the comparisons.

Did you try my procedure?

Hi, yes I have tried it today and so far everything seems great, thanks!

1 Like