Help to prevent Dividing by zero?

I have the following code in a bind value on a form (see below),

the problem I have is some of the values can be zero (2-5) which results in an error due to it dividing by zero, I need to be able to check them and if they are zero run a different calculation to exclude them.

Could anyone please point me in the right direction.

dmx-bind:value="(((inp_Income1Amount.value.toNumber() / inp_Income1PaymentSchedule.value.toNumber()) + (inp_Income2Amount.value.toNumber() / (inp_Income2PaymentSchedule.value.toNumber()) + (inp_Income3Amount.value.toNumber() / inp_Income3PaymentSchedule.value.toNumber()) + (inp_Income4Amount.value.toNumber() / inp_Income4PaymentSchedule.value.toNumber()) + (inp_Income5Amount.value.toNumber() / inp_Income5PaymentSchedule.value.toNumber()))) * 30)"

Can you put in a condition first which checks if the value != 0 and then do the next step if it’s true?

1 Like

Thanks for your help, I understand I need to do that, I am just not clear on how to achieve that.

I have been searching do I need to use a ternary?

Are you doing this client-side? If so, I would create a variable and store the result there. Then you can use a flow so you can include a condition on that variable.

If you’re doing this server-side then you use the Condition action.

thanks for putting me on the right track

I would tend to do all that kind of maths on the server side, either in your server action or using a database view. The client side really slows down fast if you try to do anything too complex.

You also have more logic available on the server side to check for zero values and decide what result you then want to return.

Good luck!
Antony.