App Connect Data Formatter Support Product Page

Solved

Weird math problem.

Reported 24 Feb 2018 03:29:44
1
has this problem
24 Feb 2018 03:29:44 Lee Woolsey posted:
I have a form that does some fairly simple math. It counts a number of events entered, than applies an additional fee based on the number of events. The additional fee is not uniform ( +0 for 1 event, +10 for 2 events, +20 for 3 events, +25 for 4 events, and +30 for 5 events.) the calculations work perfectly for 1, 2, 3 & 5 events, but when the event total is 4, the result is +230!!! It doesn't matter which events I add or subtract, when it hits 4 tthe total skyrockets, then return to expected at 5.

I have no clue why this would be.


<div id="totalFees">
	<input name="totalevents" type="hidden" id="totalevents" dmx-bind:value="selectKata.value.toNumber() + selectWeapon.value.toNumber() + selectSpar.value.toNumber() + selectBlocker.value.toNumber() + selectGrapple.value.toNumber()">
	<input name="fee_addevent" type="hidden" id="inp_fee_addevent" dmx-bind:value="totalevents.value.replace('1', '0').replace('2', '10').replace('3', '20').replace('4', '25').replace('5', '30')">
	<input name="fee_base" type="hidden" id="inp_feebase" value="45.00">
	<input name="fee_total" type="hidden" id="inp_feetotal" dmx-bind:value="inp_feebase.value.toNumber() + inp_fee_addevent.value.toNumber()">
        [h3]Events Entered: <strong>{{totalevents.value}}</strong> Additional Event Fee: <strong>{{inp_fee_addevent.value.toNumber().formatNumber(2, ".", ",")}}</strong> Total Due: <strong>{{inp_feetotal.value.toNumber().formatNumber(2, ".", ",")}}</strong>[/h3]
</div>

Replies

Replied 26 Feb 2018 08:22:01
26 Feb 2018 08:22:01 Teodor Kuduschiev replied:
Hi Lee,
I will try to recreate this issue locally and see what is wrong.
Replied 26 Feb 2018 18:10:31
26 Feb 2018 18:10:31 Lee Woolsey replied:
Any luck recreating the issue? (I've tried it with and without quotes around the parameters)
Replied 26 Feb 2018 18:31:48
26 Feb 2018 18:31:48 Teodor Kuduschiev replied:
Hi Lee,
The issue is caused because 25 contains 5, and because 5 is replaced with 30, and then 25 = 2 + 30 = 230.

I will look for a better solution instead of using replace here.
Replied 26 Feb 2018 18:49:15
26 Feb 2018 18:49:15 Lee Woolsey replied:
I suspected the 5 was the culprit, but did not associate it with the next replace statement. Interesting.

A good old-fashioned 'case' or 'switch' statement would be handy!

I changed the order, and it works correctly now ( i.e. put in this order .replace('5', '30').replace('4', '25')

I would be interested in a more elegant solution.
Replied 26 Feb 2018 19:10:58
26 Feb 2018 19:10:58 Teodor Kuduschiev replied:
Hi Lee,
You can just use:


Quotedmx-bind:value="(totalevents.value == 1).then('0') || (totalevents.value == 2).then('10') || (totalevents.value == 3).then('20') || (totalevents.value == 4).then('25') || (totalevents.value == 5).then('30')"

Replied 26 Feb 2018 19:30:53
26 Feb 2018 19:30:53 Lee Woolsey replied:
That works excellent. Thank you. I was not aware that was an option, can you point me to documentation on that function?
Replied 27 Feb 2018 08:17:09
27 Feb 2018 08:17:09 Teodor Kuduschiev replied:
Hi Lee,
I am not sure this is documented, but i will check and add such a tutorial when updating the documentation.

Reply to this topic