Get decimal point to work

I am working on a calculator app. Getting the decimal point to appear once within an input is proving difficult. Also, I enabled the NumberOnly property in the textBox component. Here are the necessary blocks.

The issue is whenever I press the decimal point more than once on the app, it will appear more than once like 3.....5 instead of just 3.5

There is a text CONTAINS block.

If the global variable CONTAINS '.' do not JOIN a '.' to it.

1 Like

Appreciate your reply
This is the new block I used, but it is not working.

It will add two decimal points to firstNum and it won't show any decimal point on secondNum even if there is point in firstNum.

The NOT block can't work with the JOIN block.

Try this (these blocks should be draggable)

The three tests in the clock timer:

  1. Removes a second decimal point if entered immediately after the first
  2. Adds a 0 if number started with decimal point
  3. Removes any decimal points after the first one, anywhere else in the "number"

Doesn't handle where number has a decimal point at the end with nothing after it, I will leave that for you to handle once the number is submitted....

I don't understand how your two variables firstnum and secondnum are meant to work together.

Could you explain them?

Just like 1.4 + 4.6;
1.4 is firstNum and 4.6 is secondNum

When the decimal point button is clicked, which variable should be updated with it?
Surely not both of them at a time.

How do you decide which variable is receiving input?

On further thought, I think you are over-burdening TextBox1.

You might find it simpler to save number1, the operator, and number2 in a list, and making only the last item in the list editable.

Is this text typed into a text box? This is how you type?

1,4 + 4,6

And you don't want someone to enter, for example,

1,,4 + 4,,6

Exactly

The variables are firstNumber, operator, secondNumber

  1. Assume a number is clicked "5", it will be saved as a firstNumber. But if decimal point is clicked immediately, then it will be "5." any number that come next say "4".
    So, firstNumber will be updated to 5.4

  2. Then an operator is clicked, say + for addition, automatically, the next number after operator will always be the secondNumber.

  3. The secondNumber can be a whole number or number with decimal say. So if 4 is pressed, then follow it with decimal point with another number say "7", secondNumber will be updated to 4.7.

The textBox is only displaying the figure. Even I initially make used of label but when decimal point issue arises, I changed it to textBox with the hope of making the numberOnly property true to solve the issue.

On List:
I am using the app to teach kid about how to use app inventor so we aren't yet treating list.

Do you have buttons for the digits 0-9 also, and a Clear (<[X]) button too?

Then you would not need to use a TextBox, instead use only a Label for full control over the input.

Regenerate the Label after each button press from your 3 variables num1, operator, num2.

If the operator is empty, apply input logic to num1, else apply input logic to num2.

d6b81b4ab0a8ad458a2019902490601e16b205c9_2_689x368

Your original code was very close.
(edit - no, the elseif is in the wrong place. It should be on the outer if. - ABG)

That blank text box may or may not have a blank in it. It's impossible to tell without trying to type in it. AI2 trims blanks, so it might be a zero length text, or a single blank.

An is empty test would be safer, hopefully covering both cases.

Here are blocks for the variable maintenance.

I did not add the procedure call to build the Label.Text from the three variables.
I did this in pre-generic style, for beginners.




initialize global num2 to
initialize global operator to
initialize global num1 to

P.S. This app is fodder for procedures and procedure parameters when you get to those topics.

Definitely, I have buttons for all other number.

Sorry I bothered....

Your post will still be there for their benefit when they get around to studying lists and generic blocks.

It will not go to waste.

Thanks for the help.

But whenever I clicked the period button again another will appear like "5.6."
Here is the aia file.

Please, help look into the inverse button too.
Calculator.aia (10.4 KB)

Thanks

Oh! Your response is appreciated. I will definitely need it later when implementing it using list.


This is the replacement for your decimal point button code.

Drag it into the Blocks Editor and remove the original.