Help ensure data accuracy with calculations.
Calculations are one of the more commonly used features of web forms. They help ensure that information collected is accurate, and can be used in many places and contexts within a form.
Calculations can be applied:
Generally, when we say "calculation", we're referring to creating new data by applying mathematics to other other data using "formulas" or "expressions". The available options are almost limitless, so for this topic, we'll focus on a simple scenario.
For additional information and a list of operators and functions, see the Formulary in the reference section.
Let's say your client's form includes an option to claim for labour or services ($claimedAmount) based on the number of hours worked ($numberHours) multiplied by the hourly rate ($hourlyRate) applicable for that work.
Depending on business rules that apply, this could be a standard or conditional calculation. In both cases, the calculation is applied to the control $claimedAmount.
Standard calculations are the most common and use typical mathematical operators such as addition (+), subtraction (-), multiplication (*) and division (/).
A calculation for the value of work performed would be:
$numberHours * $hourlyRate
If these hours are taxable and you wanted to calculate the tax in a separate control, you could write:
$numberHours * $hourlyRate * $provincialTaxRate
While you could express the current provincial tax as a number (0.05), the rate could change in the future. See Form control for information on setting reference variables.
Conditional calculations are the next most common type and use the if/then/else construction to describe when to apply a calculation. Let's say that the work claim is up to $1,500 maximum. You can use a conditional calculation to enforce that limit:
if ($numberHours * $hourlyRate) < 1500.00 then ($numberHours * $hourlyRate) else 1500.00
In plain language, this calculation says, "if the claimed amount is under $1,500.00, then calculate the claim, otherwise claim $1,500."