# Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Calculated Fields Forms, the most misunderstood operations

## Calculated Fields Forms, the most misunderstood operations

Very popular and important operations in the plugin that can result hard to understand, like conditional statements, rounding operations, distance estimation, date time operations.

The plugin includes many operations, but there are some of them very useful, but that for some reasons they are difficult to understand by the users. I'll list them, and the module where are located, such as some examples of their use.

Before starting with the operations, it is very important to understand that javascript is a case sensitive language, so, the upper and lowercase matter.

##### The conditional operation "IF" in the Logical module.

Please do not confuse with the conditional statement "if" in Javascript.

This operation requires three parameters: the condition, the value to return if the condition is true, and the value to return if the operation is false.

Ex: Assuming you want to return two times the value of the fieldname2 field `(2*fieldname2)` if the value of the fieldname1 is less than or equal to 10, and three times the value of the fieldname2 field `(3*fielname2)` if the value of the fieldname1 field is greater than 10.

``````IF(fieldname1<=10, 2*fieldname2, 3*fieldname2)
``````

This equation can be implemented using the conditional statement "if", but will require the structure of function a the "return' instruction:

``````(function(){
if(fieldname1<=10)
{
return 2*fieldname2;
}
else
{
return 3*fieldname2;
}
})()
``````

But what to do if there are other alternatives. To return `3*fieldname2` if the value of the fieldname1 field is between 10 and 100, and `4*fieldname2` if the value of the fieldname1 is greater than or equal to 100

Surprise !!! the IF operations can be nested.

`````` IF(fieldname1<=10, 2*fieldname2, IF(AND(10<fieldname1, fieldname1<100), 3*fieldname2, 4*fieldname2))
``````

Note I'm using a new operation: AND that returns true if all its parameters are true, but if at least one of parameters is false, it returns false too. There are other logical operations like: OR, NOT, IN that you can check in the documentation page of the plugin.

The same equation using the conditional statement "if"

``````(function(){
if(fieldname1<=10)
{
return 2*fieldname2;
}
else if(10<fieldname1 && fieldname1<100)
{
return 3*fieldname2;
}
else
{
return 4*fieldname2;
}
})()
``````

Note that now I'm using the "and" operator of pure javascript that is represented by the double ampersand: &&

##### The rounding operations

There are multiple operations for rounding in the plugin, but the most repeated question between the users is:

How to display the result with only two decimals?

The answer is simple, the PREC operation (for precision). The PREC operation requires two parameters, the number to round, and the number of decimals in the result.

Ex: `PREC(4.4352,3)` returns 4.435 `PREC(4.4352,2)` returns 4.44 `PREC(4.4352,1)` returns 4.4

But, what to do if you want the result of the equation `fieldname1/fieldname2` with only two decimals? In this case should be used PREC as the outermost operation, after evaluate the mathematical equation: `PREC(fieldname1/fieldname2, 2)`

##### A date/time operation

The date/time module is distributed with the Developer and Platinum versions of the plugin. This module includes multiple date/time operations, but there is one that might resulting hard to understand, DATEDIFF

`DATEDIFF(date_time, date_time, date_time_format, to_calcuate)`

• The first and second parameters are date/time fields, date/time objects, or texts with date/time format.
• The third parameter is a text with the date/time format of previous parameters, for example: `'dd-mm-yyyy hh:ii'`, `'yyyy/mm/dd'`, `'mm/dd/yyyy'`, etc.
• The fourth parameter is a character the represents the difference to calculate: `'y'`, `'m'`, `'d'`, `'h'`, `'i'` or `'s'`

This operation always return an object with the properties: years, months, days, hours, minutes, seconds.

Ex: For getting the number of months between two dates:

``````DATEDIFF('2013-10-27', '2012-06-22', 'yyyy-mm-dd', 'm')['months']
``````

Result: 16

But if you want get the difference with the format: X years, Y months and Z days, the previous equation can be implemented as follows:

``````(function(){
var diffObj = DATEDIFF('2013-10-27', '2012-06-22', 'yyyy-mm-dd', 'y');
return diffObj['years']+' years, '+diffObj['months']+' months and '+diffObj['days']+' days';
})()
``````
##### The distance between two address

The Developer and Platinum versions of the plugin are distributed with the Distance module, where is included the DISTANCE operation.

This operation is very straightforward, it requires four parameters:

`DISTANCE(address_a, address_b, unit_system, travel_mode)`

The first and second parameters are the addresses, the third parameter is the one of values: `'km'` or `'mi'` for kilometers or miles respectivaly, and the fourth parameter would be one of following alternatives: `'DRIVING'`, `'BICYCLING'`, `'TRANSIT'`, or `'WALKING'`

So, what would be the difficult then?

The API Key, the DISTANCE operation uses the Google APIs, and Google requires you generate an API key to use their APIs from the websites' domains. The following Google APIs should be enabled with your API Key:

• Maps JavaScript API
• Distance Matrix API
• Geocoding API

Once the API Key is generated the simpler way to pass it to the form, would be including an additional attribute in the form’s shortcode, called googleapikey with the API Key, for example:

`[CP_CALCULATED_FIELDS id="1" google_api_key="<enter the key here>"]`