Is there any Block in the App Inventor2 to turn 'True' into 1 and 'False' into 0 (zero)?

When I use Excel, I can create a formula like:

A1 = ABS (2 = 2)

Because (2 = 2) will return a result 'True'... and the Absolute of this is transformed into a 1

In turn, A1 = ABS (2 = 3) will return me a Zero...

Because the result of (2 = 3) will be 'False'... and when taking the absolute, Excel transforms the result into 0 (zero)

This is particularly useful for creating extensive formulas that would need many If-Then-Else within the formula itself, without having to use If-Then-Else »

=DATA($B$4-ABS(J3<(78-ABS(G3="N")));3;19)

But in the App Inventor2 the [Absolute] Block does not allow to fit into its slot any expression that returns True or False...

Is there any way to turn a True into 1 and turn a False into 0 (Zero), mathematically, without having to use If-Then-Else?

TIA :pray:

Lito

@>-->---

Here is one method, there are bound to be others....

2 Likes

Tks :pray:

Is multiplication * 1 to stop being a string and returning a mathematical result?

Lito

@>-->---

Yes, but it doesn't actually matter in AI2, it will treat "1" and 1 the same way, as a number.

1 Like

Is there no possible trick so that Absolute can accept a boolean?

Lito

@>-->---

image

the booleans: true and false, are not numbers, therefore you cannot use the absolute block here.

Wait for the mathematicians to come along...

1 Like

What's wrong with
controls_choose
?

1 Like

The amount of comparisons that are needed as to insert in a more complex formula ... that's all! ^^

=DATA($B$4-ABS(J3<(78-ABS(G3="N")));3;19)

@>-->---

The mathematicians will have to explain why Excel allows the Absolute of a Boolean ...

Now I don't know if I found a shortage in AI2 or an Excel bug! :melting_face:

@>-->---

image

ABS

2 Likes

From your answer I was doing some tests... and I found that this also works fine »

image

This opened my mind... and even though I found a form that uses If-Then-Else' (which was not the original proposal) I believe that in this way is possible to insert several comparisons within a single formula...

... without having to use the If-The-Else block in its original format

image

... since it allows the result to be mathematically manipulated, combined with other parameters of the formula.

Thanks @ABG :pray:

Lito

@>-->---

1 Like

Both Excel and Google Sheets have an ABS() function which will convert TRUE/FALSE to 0/1, this is simply how the function is programmed. AI2 is not Excel or Google Sheets. In AI2 the absolute block will only work with numbers. No mystery. A solution to this is provided by using the alternate if-then-else block as has been shown.

image

1 Like