IF AND conditional
-
Hi,
can you please tell me how can I calculate a field according to the amount input?I have different calculations to do for 7 different amount intervals.
(150*1.19)
((fieldname3*.022)*1.19)
((((fieldname3-15001)*0.016)+330)*1.19)
((((fieldname3-30001)*0.013)+580)*1.19)
((((fieldname3-60001)*0.009)+970)*1.19)
((((fieldname3-300001)*0.0065)+3130)*1.19)
((((fieldname3-600001)*0.0044)+5080)*1.19)The 7 intervals are:
1-7000
7000-15000
15000-30000
30000-60000
60000-300000
300000-600000
above 600000Thank you
-
Hello @luciche
I’m sorry, I don’t understand your piece of code. However, I thing you want something similar to:
(function(){ var v = fieldname3; if(v <= 7000) return 150*1.19; if(v <= 15000) return fieldname3*.022*1.19; if(v <= 30000) return ((fieldname3-15001)*0.016+330)*1.19; if(v <= 60000) return ((fieldname3-30001)*0.013+580)*1.19; if(v <= 300000) return ((fieldname3-60001)*0.009+970)*1.19; if(v <= 600000) return ((fieldname3-300001)*0.0065+3130)*1.19; return ((fieldname3-600001)*0.0044+5080)*1.19; })()
Best regards.
That worked, thank you for you fast help.
Can you please tell me how can I limit to 2 decimals the code above?
Thank you again.
Hello @luciche
You should use the PREC operation with your equation: PREC(X,Y) returns the number X rounded with Y decimals.
PREC((function(){ var v = fieldname3; if(v <= 7000) return 150*1.19; if(v <= 15000) return fieldname3*.022*1.19; if(v <= 30000) return ((fieldname3-15001)*0.016+330)*1.19; if(v <= 60000) return ((fieldname3-30001)*0.013+580)*1.19; if(v <= 300000) return ((fieldname3-60001)*0.009+970)*1.19; if(v <= 600000) return ((fieldname3-300001)*0.0065+3130)*1.19; return ((fieldname3-600001)*0.0044+5080)*1.19; })(), 2)
Best regards.
What an awesome support you provide.
I’ve just left you a well deserved 5* review.Thank you very much.
I also have 1 radio button, fieldname16, with option A and B, and I want to calculate .0015 for option A and .05 for option B
basically when option A is selected to calculate like this:
PREC((function(){
var v = fieldname2;if(v <= 100) return 0;
if(v <= 40000) return 60;
if(v > 40000) return fieldname2*.0015;
if(v > 40000) return fieldname2*.0015;
})(), 0)and when B is selected to calculate like this
PREC((function(){
var v = fieldname2;if(v <= 100) return 0;
if(v <= 40000) return 60;
if(v > 40000) return fieldname2*.005;
if(v > 40000) return fieldname2*.005;
})(), 0)Thank you
Hello @luciche
The equation can be implemented simply as follows:
PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; return fieldname2*IF(fieldname16 == 'A', 0.0015, 0.05); })(), 0)
Pay attention: I’m using in the equation the javascript conditional statement
if
and theIF
operation in the plugin. Javascript is a case sensitive language, please, do not confuse them.Best regards.
It’s a bit strange that works in the preview mode fine, but doesn’t work on the live site https://i.imgur.com/Ji756wR.gif
Here is the live site: https://tinyurl.com/ya4rzoqj
the field in question is “Taxa ANCPI-OCPI”
and it has this codePREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; return fieldname2*IF(fieldname17 == 'Fizica', 0.0015, 0.005); })(), 0)
with any amount above 40000 and the “Juridica” selected in the “Cumparatorul este persoana*” it returns 0 when it should multiply with 0.005 (0.5%)
Hello @luciche
The issue is simple. You’ve active a plugin on your website for optimizing. This plugin has enabled the option to combine and minify the javascript code. However, its minification algorithm contains errors and breaks the code of other plugins (In our case, it breaks the code of the IF operation).
You have two alternatives: You can disable the option that minifies and combine the javascript files in the settings of the optimization plugin, or you can edit the equation as follows:
PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; return fieldname2*((fieldname17 == 'Fizica') ? 0.0015 : 0.005); })(), 0)
However, I cannot guarantee the plugin you are using for optimizing won’t be breaking the code of other operations or the code of other plugins.
Best regards.
It worked this way.
Last question promise,
can you please tell me how can I multiply the code below with fieldname23 only when fieldname16 has option A selected (radio button)PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; return fieldname2*((fieldname17 == 'Fizica') ? 0.0015 : 0.005); if(v <= 300000) return ((fieldname2-60001)*0.009+970)*1.19; })(), 0)
Thank you so much for your support, it helps me a lot.
Hello @luciche
The order of the instructions in the equation matters. In your equation, the conditional statement
if(v <= 300000)
won’t be reached because of thereturn
instruction above move the execution flow outside the equation.So, the correct equation would be:
PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; if(v <= 300000) return ((fieldname2-60001)*0.009+970)*1.19; return fieldname2*((fieldname17 == 'Fizica') ? 0.0015 : 0.005); })(), 0)
Concerning the question about the fieldname16 and fieldname23 fields, the equation can be edited as follows:
PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; if(v <= 300000) return ((fieldname2-60001)*0.009+970)*1.19; return fieldname2*((fieldname17 == 'Fizica') ? 0.0015 : 0.005); })()*IF(fieldname16 == 'A', fieldname23, 1), 0)
Best regards.
Doesn’t seem to do anything
This works fine
PREC((function(){ var v = fieldname2; if(v <= 450000) return 0; if(v > 450000) return (fieldname2-450000)*0.03; })(), 0)
but this doesn’t multiply it with fieldname23 when A is selected in fieldname16
PREC((function(){ var v = fieldname2; if(v <= 450000) return 0; if(v > 450000) return (fieldname2-450000)*0.03; })()*IF(fieldname16 == 'A', fieldname23, 1), 0)
Am I doing something wrong?
Hello @luciche
I’m sorry, I always forget you have an optimization plugin that breaks the code of other plugins, in special the code of the “IF” operation. The correct would be:
PREC((function(){ var v = fieldname2; if(v <= 450000) return 0; return (fieldname2-450000)*0.03; })()*((fieldname16 == 'A') ? fieldname23 : 1), 0)
Best regards.
This doesn’t work
PREC((function(){ var v = fieldname2; if(v <= 450000) return 0; return (fieldname2-450000)*0.03; })()*((fieldname16 == 'A') ? fieldname23 : 1), 0)
but this works
PREC((function(){ var v = fieldname2; if(v <= 100) return 0; if(v <= 40000) return 60; return fieldname2*((fieldname17 == 'Fizica') ? 0.0015 : 0.005); })()*((fieldname16 == 'A') ? fieldname23 : 1), 0)
Does it matter if fieldname23 is a Hidden field?
ps Do you have Revolut? I want to show my gratitude for all your help with this.
Hello @luciche
I’ve visited your web page, and the equations are working as should. Why do you say the first equation is not working? Which values are you entering, and which should be the result?
About the fieldname23 field, it does no matter if the field is a hidden control or not. However, if your field has becomes hidden by dependencies (https://cff.dwbooster.com/blog/2020/03/01/dependencies/) its value would be zero, because the value of the dependent and deactivated fields is zero.
Best regards.
- The topic ‘IF AND conditional’ is closed to new replies.