• Resolved rphrus

    (@rphrus)


    Hi,
    I would like help with the formula below:
    fieldname1 = number
    calculated field with numerical result from the equations below:
    If fieldname1 entered is from 1 to 20 mg then multiply number entered by factor of 4. For example, if number entered is 1 then 1×4. The calculated field will show 4. If number entered is 2 then 2×4. the calculated field will show 8, etc.
    If fieldname1 entered is from 21 to 40 mg then multiply number entered by factor of 8
    If fieldname1 entered is equal to or greater than 41 to 60 mg then multiply number entered by factor of 10.
    Thank you.

    • This topic was modified 2 years, 4 months ago by rphrus.
    • This topic was modified 2 years, 4 months ago by rphrus.
    • This topic was modified 2 years, 4 months ago by rphrus.
    • This topic was modified 2 years, 4 months ago by rphrus.
    • This topic was modified 2 years, 4 months ago by rphrus.
    • This topic was modified 2 years, 4 months ago by rphrus.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @rphrus

    Thank you so much for using our plugin.

    You can implement the equation as follows:

    (function(){
    var n = fieldname1;
    if(n < 21) return n*4;
    if(n <= 40 ) return n*8;
    if(n <= 60 ) return n*10;
    })()

    Please, note that your formula description does not consider the case when 60 < n, assuming you want to multiply n by factor 12, the equation would be:

    `(function(){
    var n = fieldname1;
    if(n < 21) return n*4;
    if(n <= 40 ) return n*8;
    if(n <= 60 ) return n*10;
    return n*12;
    })()’

    There is no unique implementation. You can nest multiple IF operations. Javascript is a case-sensitive programming language. Please, do not confuse the IF operation in the plugin with the if conditional statement used above:

    fieldname1*IF(fieldname1<21, 4, IF(fieldname1<=40, 8, IF(fieldname1<=60, 10, 12)))

    Or, you can use the javascript ternary operator (condition) ? x : y :

    fieldname1*(fieldname1<21 ? 4 : (fieldname1<=40 ? 8 : (fieldname1<=60 ? 10 : 12)))

    Best regards.

    Thread Starter rphrus

    (@rphrus)

    Thank you so much. The only problem I have is the calculated field always display 0 when I did not enter anything in the fieldname1 yet. I want it blank before I start entering in the number in the fieldname1.

    Plugin Author codepeople

    (@codepeople)

    Hello @rphrus

    You can check the value of the fieldname1 field before evaluating the mathematical operations:

    (function(){
    var n = fieldname1;
    
    if(!n) return '';
    
    if(n < 21) return n*4;
    if(n <= 40 ) return n*8;
    if(n <= 60 ) return n*10;
    return n*12;
    })()

    Please not that I included the if(!n) return ''; line of code before the mathematical operations.

    Best regards.

    Thread Starter rphrus

    (@rphrus)

    Great!
    Thank you very much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘multiplication by a factor within a range of numbers’ is closed to new replies.