• Resolved tilou

    (@tilou)


    Hi

    I want to return a ratio such as 2:3
    I’ve found this code online
    How would I put that in the equation field

    function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)}

    Thanks
    Jean-Lou

    • This topic was modified 6 years, 8 months ago by tilou.
    • This topic was modified 6 years, 8 months ago by tilou.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @tilou,

    The easiest way to use this equation, assuming that X, and Y are the fields: fieldname1 and fieldname2 respectively (you should use the real fields names in your form), the equation to associate with the calculated field would be as simple as:

    
    (function(){
    function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)};
    return ratio(fieldname1,fieldname2);
    })()
    

    and that’s all.
    Best regards.

    Thread Starter tilou

    (@tilou)

    Great thanks! It works!
    Hope am not abusing your time can you please help me with this one?
    function gcd(a,b) {if(b>a) {temp = a; a = b; b = temp} while(b!=0) {m=a%b; a=b; b=m;} return a;}

    • This reply was modified 6 years, 8 months ago by tilou.
    Plugin Author codepeople

    (@codepeople)

    Hello @tilou,

    I don’t understand what do you need, because the solution is just the same. Even if you want create your functions globally, to use them from multiple equations, you simply should insert a “HTML Content” field in the form with the following piece of code as its content:

    
    <script>
    window['gcd'] = function(a,b) {if(b>a) {var temp = a; a = b; b = temp} while(b!=0) {var m=a%b; a=b; b=m;} return a;};
    window['ratio'] = function (x,y) {var c=gcd(x,y); return ""+(x/c)+":"+(y/c)};
    </script>
    

    Now, to use this operations in the equations is as simple as:

    
    ratio(fieldname1,fieldname2)
    

    or

    
    gcd(fieldname5,fieldname9)
    

    I’m sorry, but the support service does not covers the implementation of the users projects (forms or formulas). If you need additional help implementing your project, I can offer you a custom coding service from my private website:

    https://cff.dwbooster.com/customization

    Best regards.

    Thread Starter tilou

    (@tilou)

    Great thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Function: Calculatre Ratio from 2 numbers’ is closed to new replies.