Doing maths

Hi, me again

is there a way to do math on the App side?

Basically I show a customer an estimated price based on their transport booking. Pricing is volume/weight based (which ever is greatest). So, for exampe I might charge 1.90 per kg and 95 per m3.

A client books on 5m3 that weighs 500kg.

so in weight that is 950 and in volume it is 450. I need to show to the client the greater value.

I have the values pulled in from the DB.

and I can do basic maths on it like this:
(weight price)

{{sc_UserJobSummary.data.q_UserJob_sel[0].kg_price_deliverydate * sc_UserJobSummary.data.q_UserJob_sel[0].weight}} 

(volume price)

{{sc_UserJobSummary.data.q_UserJob_sel[0].m3_price_deliverydate * sc_UserJobSummary.data.q_UserJob_sel[0].volume}}

but I only want to show the client the higher price

so something like an if xxx >= yyy show xxx else yyy

cant figure out how I would do this in wappler. Hope this makes some sense. Dont want to start trying to hack it in if it is possible in wappler :slight_smile:
Thanks
Darren

You can use two variables to make the things easier (just add them in the app structure). So let’s say you call them varWeight and varVolume.

So your weight var will be:

<dmx-value id="varWeight" dmx-bind:value="sc_UserJobSummary.data.q_UserJob_sel[0].kg_price_deliverydate * sc_UserJobSummary.data.q_UserJob_sel[0].weight"></dmx-value>

And your volume var will be:

<dmx-value id="varVolume" dmx-bind:value="sc_UserJobSummary.data.q_UserJob_sel[0].m3_price_deliverydate * sc_UserJobSummary.data.q_UserJob_sel[0].volume"></dmx-value>

So in order to compare them and show the bigger value you can use the ternary operator:

{{varWeight.value > varVolume.value ? varWeight.value : varVolume.value}}

which means -> if varWeight.value is bigger than varVolume.value, then show varWeight.value otherwise (if it is not biiger) show varVolume.value

Hope this makes sense :slight_smile:

thanks, the theory all makes sense :slight_smile: I have setup the variables and added those values, but, how do I setup the ternary? I tried clicking the lightning arrow on my paragraph and copying into the code section but it didnt like it. thanks

There is a ternary operator formatter available in the formatters. But you can just copy and paste the code I provided

yeah i got there in the end, it is because I tried copying the value code into the value box in the App tree, copying it into source worked and setup the variable.

You are an absolute super star, thanks for that.

Best regards
Darren

1 Like

can I get the ternary result into a variable like this:

<dmx-value id="varEstimate" dmx-bind:value="varWeight.value > varVolume.value ? varWeight.value : varVolume.value"></dmx-value>

Yes, then you can just use {{varEstimate.value}} to display the value on the page

doesnt see to work, I just get a spinning icon at side of the variable in the App

1

although it does contain the right data

Probably because your values are not available yet (in design view). Please just bind the data manually on the page.