Search code examples
app-inventor

App Inventor 2 - Separating numbers


In my app I will have several lines of numbers from 4 to 10 characters in length. How would I go around separating the generated numbers every 3 numbers (at thousand, million, billion).

I will not know the length of the numbers beforehand so the same .text label can sometimes be longer or shorter.

e.g. from 1234567 12345 1234567890

to 1,234,567 12,345 1,234,567,890

The separators can be a space, a dot or a comma (any is fine). I am quite familiar with AI2 but can't figure this one out.

EDIT: Seems like I managed to segment the text (yay!) enter image description here

What would I have to do now if I have let's say 10 labels with all different lengths? It would be extremely excessive to check length of each label using an if and then segmenting it using corresponding length option. Is there a way to shorten the process?

Thanks a lot :)


Solution

  • You can write your own custom procedure, for example see the following App Inventor Classic example, which uses a for range loop:

    The algorithm works like this (example=2134256):
    result after first loop: ,256
    result after second loop: ,134,256
    then add the remaining digits of the number, in this case 2
    result in the end: 2,134,256

    Y

    Edit: For App Inventor 2, you also have to use a procedure with result. Also let me recommend to use a local variable result instead of a global variable.

    enter image description here