Calculation fields in Castor allow you to solve different mathematical problems by using the variables from your eCRF.

**Common applications of calculations**

Calculations can be used for simple mathematical operations, such as calculating the body mass index from two variables - height and weight. The calculation fields can also be used for more complex operations, such as calculating a score from multiple questions in a survey, or defining diagnosis groups based on a selected diagnosis.

You can also use calculations for non-mathematical problems, such as cross-checking different variables within your eCRF, e.g. when you want to ensure that the date of discharge is later than the date of admission. If you want to use multiple dependencies - to make a field appear if two or more conditions are met, you can also use calculations.

Our online manual also contains examples of calculations that you can use. You will need to adjust the calculations to your own study. You can use information from other fields by using the variable name of the fields in the calculation. Variable names are always between braces ({}) like this: {variable_name}.

**Referencing study variables**

You can access the value of the field by putting the variable name of the field between braces:

{variable_name_here}

When contacting Castor EDC support with questions about calculations, we recommend adding the stored calculation link to your question. Accessing the calculation details can help us answer your question.

If you want to use the **text**** **value of a field, enter the variable name and braces in quotes:

'{variable_name_here}'

### Form Exchange

A great way to get started with calculations is by using our Form Exchange calculation templates. Learn how to use it here.

**Calculation Helper**

You can use the calculation helper tool to check the syntax and output of your calculations. The find the calculation helper by clicking on the **Support **button:

**Define your variables**: In the field on the left you have to define the variables that you are going to use (see the example). In the example the patient's weight is 80 and their height is 1.80.**Calculation templat****e**: In the field on the right you can build your own calculation (in this case with the variable {pat_weight} and {pat_height}).**Update calculation**: Use this button to refresh the calculation template.**Save calculation**: Clicking this button will generate a link and copy it to clipboard in case you would like to share or save the calculation to use later.**Syntax check**: If there are any issues with your script, they will appear in this field.**Final formula**: Here you will see the calculation template with the actual values of the variables.**Result**: The final outcome of the calculation will be displayed in this field.

When contacting Castor EDC support with questions about calculations, we recommend adding the stored calculation link to your question. Accessing the calculation details can help us answer your question.

### JavaScript

Castor uses JavaScript to solve your Calculation Template. If you are a beginner, we suggest you have a look at the general calculation templates and calculation templates using date fields - you can find most commonly used templates here and you can simply copy the template into your own calculation field and adjust it as necessary.

**Standard arithmetic operators in JavaScript:**

+ |
Addition |

- |
Subtraction |

* |
Multiplication |

/ |
Division |

% |
Modulus (division remainder) |

**Different mathematical functions in JavaScript:**

Method |
Description |
---|---|

Math.abs({variable}) |
Returns the absolute value of the variable |

Math.ceil({variable}) |
Returns variable, rounded upwards to the nearest integer |

Math.floor({variable}) |
Returns variable, rounded downwards to the nearest integer |

Math.log({variable}) |
Returns the natural logarithm (base E) of the variable |

Math.max({variable1}, {variable2}, {variable3}, ...) |
Returns the number with the highest value from variable1, variable2 and variable3 |

Math.min({variable1}, {variable2}, {variable3}, ...) |
Returns the number with the lowest value variable1, variable2 and variable3 |

Math.pow({variable},x) |
Returns the value of variable to the power of x |

Math.round({variable}) |
Rounds variable to the nearest integer |

Math.sqrt({variable}) |
Returns the square root of variable |

You can find more mathematical functions here.

**Set conditions (if statements) in JavaScript:**

You can use conditions to run formulas only if a variable matches your condition.

For example (open the calculation in the calculation helper):

if({variable} > 0) 100 / {variable};

You can find more information about if/else logic here.

**.toFixed(n) - Adjusting the number of digits after the decimal point**

.toFixed(n) helps to adjust the number of digits appearing after the decimal point.

For example, when calculating a BMI, it is possible to display 3 digits after the decimal point

var bmi = {pat_weight}/({pat_height}*{pat_height}); bmi.toFixed(3);

Test this calculation here.

It is also possible to add this in the end of the formula, by just putting the end result in parenthesis and then adding .toFixed(x) at the end. For example:

if({measurement_unit} == 0) { ({weight} * 2.2046).toFixed(4) }

Test this calculation here.

If you want to learn more about JavaScript, you can check out this JavaScript Tutorial.

Below is the list of the most relevant articles from the tutorial that will help you get started with JavaScript:

## Castor Support Team