The equations allowed by the plugin "Calculated Fields Form" are really powerful tools.
It is possible create simple equations like follow:
a) With simple mathematical operations:
fieldname1+fieldname2
fieldname1*fieldname2
fieldname1/fieldname2
fieldname1-fieldname2
or mathematical equations with multiple fields and fields grouping included.
fieldname1*(fieldname2+fieldname3)
b) The equations may include a group of more specific operations, some of them available as buttons in the plugin's interface, the other operations should be typed manually in the equation's editor:
Operation | Description | Demo |
abs(x) | Returns the absolute value of x | If the value of fieldname1 is -7.25, the result of: abs(fieldname1) would be 7.25 |
acos(x) | Returns the arccosine of x, in radians | If the value of fieldname1 is 0.5, the result of: acos(fieldname1) would be 1.0471975511965979 |
asin(x) | Returns the arcsine of x, in radians | If the value of fieldname1 is 0.5, the result of: asin(fieldname1) would be 0.5235987755982989 |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians | If the value of fieldname1 is 2, the result of: atan(fieldname1) would be 1.1071487177940904 |
atan2(x,y) | Returns the arctangent of the quotient of its arguments | If the value of fieldname1 is 8 and fieldname2 is 4, the result of: atan2(fieldname1,fieldname2) would be 1.1071487177940904 |
ceil(x) | Returns x, rounded upwards to the nearest integer | If the value of fieldname1 is 1.4, the result of: ceil(fieldname1) would be 2 |
cos(x) | Returns the cosine of x (x is in radians) | If the value of fieldname1 is 3, the result of: cos(fieldname1) would be -0.9899924966004454 |
exp(x) | Returns the value of E^x | If the value of fieldname1 is 1, the result of: exp(fieldname1) would be 2.718281828459045 |
floor(x) | Returns x, rounded downwards to the nearest integer | If the value of fieldname1 is 1.6, the result of: floor(fieldname1) would be 1 |
log(x) | Returns the natural logarithm (base E) of x | If the value of fieldname1 is 2, the result of: log(fieldname1) would be 0.6931471805599453 |
max(x,y,z,...,n) | Returns the number with the highest value | If the value of fieldname1 is 5 and fieldname2 is 10, the result of: max(fieldname1, fieldname2) would be 10 |
min(x,y,z,...,n) | Returns the number with the lowest value | If the value of fieldname1 is 5 and fieldname2 is 10, the result of: min(fieldname1, fieldname2) would be 5 |
pow(x,y) | Returns the value of x to the power of y | If the value of fieldname1 is 4 and fieldname2 is 3, the result of: pow(fieldname1, fieldname2) would be 64 |
random() | Returns a random number between 0 and 1 | |
round(x) | Rounds x to the nearest integer | If the value of fieldname1 is 2.5, the result of: round(fieldname1) would be 3 |
sin(x) | Returns the sine of x (x is in radians) | If the value of fieldname1 is 3, the result of: sin(fieldname1) would be 0.1411200080598672 |
sqrt(x) | Returns the square root of x | If the value of fieldname1 is 9, the result of: sqrt(fieldname1) would be 3 |
tan(x) | Returns the tangent of an angle | If the value of fieldname1 is 90, the result of: tan(fieldname1) would be -1.995200412208242 |
Pay special attention to the next two operations. The first of them, very extended its use in equations for calculations the cost of products and services, and the other one to obtain a date.
Operation | Description | Demo |
prec(x,y) | Return the x number with y decimal digits | If the value of fieldname1 is 10.33323, the result of: prec(fieldname1,2) would be 10.33 If the value of fieldname1 is 10.3365, the result of: prec(fieldname1,2) would be 10.34 If the value of fieldname1 is 10, the result of: prec(fieldname1,2) would be 10.00 |
cdate(x) | Returns the number x formatted like a Date. The number represents the number of days from Jan 1, 1970 | If fieldname1 is a date field, and its value is 3/11/2013: cdate(fieldname1+10) would be 13/11/2013 |
Sample valid equations:
To calculate the monthly payment in a lease calculator:
The fields implied are:
Load amount: fieldname1
Residual value: fieldname2
Interest rate %: fieldname3
Number of months: fieldname4
The corresponding equation for monthly payment is:
prec((fieldname1*fieldname3/1200*pow(1+fieldname3/1200,fieldname4)-fieldname2*fieldname3/1200)/(pow(1+fieldname3/1200,fieldname8)-1),2)
c)There is a huge number of equations that can't be recreated with simple mathematical operators, or the operations listed above.
Pay attention to the following sample:
In your form there are four fields: fieldname1, fieldname2, fieldname3 and the calculated field: fieldname4, but the value of fieldname4 is dependent of fieldname3 value, that is:
The value of fieldname4 will be: fieldname1+fieldname2, if the value of fieldname3 is greater than 100, or will be: fieldname1*fieldname2, if the value of fieldname3 is less than or equal to 100. Neither of previous operations, by itself, can calculate the value of fieldname4.
To solve complex cases like this, the "Calculated Fields Form" plugin allows entering javascript code directly in the equation editor, like the following sample:
(function(){
if(fieldname3 > 100) return fieldname1+fieldname2;
if(fieldname3 <= 100) return fieldname1*fieldname2;
})();
For complex equations where is required to define blocks of javascript code, you must use the following format:
(function(){
//Your code here
})();
and the return the value of that function will be the value assigned to the calculated field:
return fieldname1+fieldname2;