Doing Math - What is Wrong?

I’m trying to add two variables together.
I figured Wappler was getting confused when adding values straight out of the database, so I’ve made variables that have the same value.

But, Wappler does not want to add them together for some reason.

Is this correct?
image
The code:
image

So you can see the output…
Each variable on its own is outputting the correct value.
When added together it doesn’t work.

image

You don’t actually need to add variables to do math with database values.

But even if using variables math is really simple:

<dmx-value id="var1" dmx-bind:value="10"></dmx-value>
<dmx-value id="var2" dmx-bind:value="10"></dmx-value>
<p>{{var1.value + var2.value}}</p>

This produces 20 as a result.

Isn’t that what I’m doing above?

I rewrote it like this:

NET LIMIT SHOULD REALLY BE: 
{{rte_FA_ListALLExpenses.var_LimitForQuery.value + rte_FA_ListALLExpenses.var_ValueOfCurrLimitInListAll.value}}

Still got 10.

I tried taking off .toNumber() and you’ll see it concats them (so at least one of them is a text, but I’ll worry about that later)

image

Can you do a dmx.app.data and check what are the exact values your variables have? Also, what is the issue to use directly the database values?

I’m not 100% sure what you mean by “do a dmx.app.data”

When I do this:

<p dmx-text="rte_FA_ListALLExpenses.var_LimitForQuery.value"></p>
<p dmx-text="rte_FA_ListALLExpenses.var_ValueOfCurrLimitInListAll.value"></p>

It returns, correctly:
image


The issue with using values directly from the db… in the past when I’ve encountered some weird issue that I couldn’t solve, if I dump the value to a variable, instead of working directly from the db value, it has worked.
I couldn’t get this to work, so I dumped it into a variable first.

I believe this is typing dmx.app.data into the console in browser dev tools it will give you a object tree with all the variables values etc so you can see what the value is and wether its a number or string etc.

On a side note I haven’t used the tonumber formatter much so this is just a idea, but I see your applying it to both values what if one is already a number? not sure how Wappler would handle this could be nothing just wondering if that would throw some error

Do you see error is the browser console?

Thanks.

@Teodor

Original values:
image

image

The variables I put them in:
image

1 Like

Well one of the values is a number and the other is a string. You can’t convert a number value to a number, the tonumber formatter should not be applied to it.

2 Likes

Okay, so when I remove the .toNumber from that one it adds up correctly.

Same would apply to the originals.

rte_FA_ListALLExpenses.sc_ListALLExpenses.data.query_AllExpenses_Finance.limit was a number.
But, id_select_limitNumberOfExpensesListed.selectedValue was not.

edit: I incorrectly assumed the value from a select box would be a number.

1 Like

Input values are always strings.

2 Likes

This behaviour should be changed, it’s the only way to guarantee type safety. Applying toNumber to a Number should output a Number

The same happens in Javascript:

Number(Number(3))

This returns the Number 3

3 Likes

I agree, @Apple! It makes the most sense to have .toNumber return a number if the input is already a number.

I had already started thinking through my whole app, because I often add .toNumber to things as a ‘safety feature’.

Thanks! :smiley:

Maybe @patrick can check this.

1 Like

You should be able to use the toNumber formatter on any type, also on numbers.

Didn’t seem like the case above.

This is not the case. toNumber() only works with strings in its current form. What can work is multiplying by 1 which then always gives the answer as a number e.g. routetosomenumber.value*1

It would, however, be good to extend the toNumber formatter to return a number if it already was one.

The toNumber formatter currently returns

Formatter toNumber in expression [var1.value.toNumber()] doesn’t exist for type number

when you try to apply it to a number, maybe we can improve this.