Basic addition issue "1null" or adding 10x value

I am trying to do something pretty basic. I want two input fields: years and months. Then I want to make a third variable be years + months.

You would think this would be easy: make a flow function so that when either of the two above inputs are changed, add “Years” and “Months/12”.

However, when I try this the end variable either says “1null” or the year variable will be multiplied by 10 so that if I input 1 year and 1 month the end variable would be 11 not 1.08.

Can anyone please help? This seems like I’m missing something obvious but it’s driving me insane. All three variables (2 input and 1 output) are set to number type.

Check out please the “ADD_DURATION()” formula documentation.
If that doesn’t help ping me again.

https://docs.appgyver.com/docs/add-duration

Hi @Mihaly_Toth thanks but that’s not quite what I’m looking for. I think that makes it more complicated than I need.

Mathematically and logically speaking, all I really want is three numbers. A, B, and C. I want A and B to be inputs and C to be the output that I use in the rest of my app.

Currently I have 3 variables all of which are number types. I have 2 input components and their values are bound to number variable A and number variable B. There is a flow function on C so that when either A or B are changed, C = A+B. Currently when I input “1” into A, C becomes “1null”. When I go on to input “2” into B then C becomes “12”.

As I want it to do is add A and B so that C = A+B. If that works I can do the rest (like B/12 so that it’s months or a twelfth of a year).

Please help!

Okay, then your issue is most probably in the default behaviour of the input field.

The input field in Composer can have value of text/number, which causes it to return a text This is why:

This happens. Adding two strings ‘concatenates’ them together, hence it is 12, instead of 3.

To prevent this from happening when you have your addition formula wrap your variables into the NUMBER() formula, which will force them to be interpreted as a number and it will no longer happen.

Now just a general idea about adding values in AG.
If you want to modify the value of the first input field, the simplest what you can do is to have pageVars.firstInput and pageVars.secondInput as two page variables. The type is number. But this doesn’t matter as soon as you bind it to an input field… :frowning:
Assign the first value in the first input field and the second respectively.
On the second input field’s logic canvas you can add a “Set page variable” logic node to the “input blurred” event (for example) where inside the set page variable logic you would have your formula: (and of course select the firstInput page variable in the properties on the right side of the Composer)

NUMBER(pageVars.firstInput)+(NUMBER(pageVars.secondInput)/12)

That should solve your issue.

One further note, I’m not sure in what kind of localisation do you plan on using your application, but the “,” is not regarded as a decimal separator in Composer, so to avoid all possible scenarios we can enrich your formula with this:

NUMBER(REPLACE_ALL(STRING(pageVars.firstInput), ",", "."))+(NUMBER(REPLACE_ALL(STRING(pageVars.secondInput), ",", "."))/12)

The REPLACE_ALL() here plays a role of transforming the decimal comma to a dot as well (I frequently use it as I’m based in Hungary or work with German localization, which may very often use comma as a decimal separator…)

Hope this clarifies a little and good luck with the rest of your functionality. Keep in mind that this is just one of some quirks of AG. But once you get used to them, you can almost do lots of brilliant things.

1 Like

@Mihaly_Toth Thank you very much!

The problem was really weird because I was pretty sure I had it working a while ago but not when I tried again recently. Anyway, using NUMBER() stops the concatenation issue.

Also, thanks for the other tips. I really appreciate your fast responses and useful advice! : )