# $calc
This keyword is used to perform basic mathematic operations with numerical values from selected fields. In conjunction with the keyword $var
it's possible to reference fields that will be part of the operation.
# Installation:
cob-cli customize calc
# Usage:
$calc.[sum|multiply|subtract|divide|diffdays|diffHours|diifMinutes](var.1,var.2,...)
keyword | Description |
---|---|
$calc.sum(var.1,var.2,...) | Sum of all values from all variables |
$calc.subtract(var.1,var.2) | Calculate the difference between two variables |
$calc.multiply(var.1,var.2,...) | Multiply all values from all variables |
$calc.divide(var.1,var.2) | Divide the value of variable 1 by variable 2 |
$calc.diffDays(var.1,var.2) | Calculate the number of days between two variables |
$calc.diffHours(var.1,var.2) | Calculate the number of hours between two variables |
$calc.diffMinutes(var.1,var.2) | Calculate the number of minutes between two variables |
The keyword $calc requires numerical values in the fields asscoiated to the variables. They can be of type:
WARNING
- Both the
sum
and themultiply
options accept an indeterminate number of arguments:- Valid operation:
$calc.sum(1, 2, 3, 5, 6, 67, 4567, 567, 567, 567)
. - Another valid operation:
$calc.multiply(1, 2, 3, 5, 6, 67, 4567, 567, 567, 567)
.
- Valid operation:
- The remaining operations only accept two arguments. If we add more, the value of a field will be empty.
# Defining variables with $var
Besides constant numbers in calc operations, it is also possible to use values of other fields in a calc operation with the use of the keyword $var
. This allows the user to see reflected in real time a new result everytime a variable value is changed.
$number(2) $var.meat
$number(2) $var.fish
$number(2) $calc.sum(var.meat,var.fish)
See this example
# Scoped $var
iables
The keyword $var
also supports scopes, meaning that you can perform operations using fields of the same $var
type.
WARNING
This feature is only supported for the sum
and multiply
operations.
$number(2) $var.total.meat
$number(2) $var.total.fish
$number(2) $calc.sum(var.total)
See this example
# previous
argument
$calc
customization also supports a quick way to reference the previous value. Using the operation argument previous
you will be reference the previous field, which can be the parent field or the previous sibling field.
WARNING
The argument previous
cannot be applied to the first field of the definition. In a situation where that happen no calculations will be performed on any instance of that definition type.
See this example
# Examples:
# 1. Basic usage of $calc
Meat (kg): $var.meat
Fish (kg): $var.fish
Total (kg): $calc.sum(var.meat,var.fish)
# 2. Using scoped variables in a sum operation
Meat (kg): $var.total.meat
Fish (kg): $var.total.fish
Total (kg): $calc.sum(var.total)
# 3. Use previous
as an argument
VAT (€): $calc.multiply(var.cost,previous)
# 4. Duplicable var
iables
$var.fish and $var.meat is part of a duplicable field
# 5. Calculating differences between dates
Ticket Open: $date $var.open
Ticket Closed: $date $var.closed
Total days opened: $calc.diffDays(var.closed,var.open)d