Text not converted to number

My App uses JSON to send commands to an Arduino.
One value has to be a number, not a string. But AI2 doesn't convert the string from a textbox to a number. How can I force this?

Block:


JSON string received by Arduino (note the quotes around the TANKCAPACITY value). The ID is formatted correctly as a number, but that number is defined in the code and not retrieved from a textbox.

16:32:30.310 -> {
16:32:30.310 -> "ID": 28,
16:32:30.310 -> "CMD": "SET",
16:32:30.310 -> "SET": "TANKCAPACITY",
16:32:30.310 -> "TANKCAPACITY": "25"
16:32:30.310 -> }

Ignore the timestamps, they are not part of the message.

You can add 0 to the value to convert it to a number.

Yep, that does the job. Maybe worthwile to mention this in the documentation. I had to add debug code to Arduino to find out why it was rejecting the command. Fortunately I'm the author of the Arduino code, otherwise I would still be at a loss.

16:59:14.686 -> {
16:59:14.686 -> "ID": 28,
16:59:14.686 -> "CMD": "SET",
16:59:14.686 -> "SET": "TANKCAPACITY",
16:59:14.686 -> "TANKCAPACITY": 25
16:59:14.686 -> }

BTW, is there an easier way to verify that a number is an integer number, other than checking that it doesn't contain separators?

There certainly should be an App Inventor function for this, I will add it to our (very long) Wish List.

Meanwhile: Check if number is an integer - #16 by TIMAI2

Nice. I already implemented the first method (checking for separators in the string) but the second method (division by 1) is certainly more elegant and more accurate.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.