Advanced Math Calculations
The Calculated field of JetFormBuilder is not limited to basic mathematical operations like addition, subtraction, multiplication, and division. It supports all the Math object methods, which helps perform advanced calculations with numeric values returned by form fields. The complete list of Math properties and methods can be found in MDN Web Docs.
Below, we will cover several usage examples — rounding a number, finding min and max numbers from a row, and raising a number to a power. The described methods can be used with a numeric value returned by any of the JetFormBuilder fields. In our examples, we will use a Number field to store the numeric value.
Rounding
The Math.round() method can be used to round the specified number to the nearest integer.
To apply this method in the Calculated field, use the below formula:
Math.round(%number_field%)
Insert the formula into the Calculated field to round a numeric value of another field.
As a result, a float retrieved from another form field will be rounded in the Calculated field.
Rounding Up
The Math.ceil() method returns the smallest integer greater than or equal to the specified number.
To apply this method in the Calculated field, use the below formula:
Math.ceil(%number_field%)
Insert the formula into the Calculated field to round up a numeric value of another field.
As a result, a float retrieved from another form field will be rounded up in the Calculated field.
Rounding Down
The Math.floor() method returns the largest integer less than or equal to the specified number.
To apply this method in the Calculated field, use the below formula:
Math.floor(%number_field%)
Insert the formula into the Calculated field to round down a numeric value of another field.
As a result, a float retrieved from another form field will be rounded down in the Calculated field.
Finding the Largest and the Smallest Numbers
The Math.min() method retrieves the smallest of several numbers. On the contrary, the Math.max() method is used to retrieve the largest of numbers.
To apply the Math.min() method in the Calculated field, use the below formula:
(Math.min(%number_one%, %number_two%, %number_three%))
For the Math.max() method, use this formula:
(Math.max(%number_one%, %number_two%, %number_three%))
Insert the needed formula into the Calculated field to find the largest or the smallest number from a row of numbers.
In our example, we have three Number fields, each containing a specific number: 7, 3, 4, accordingly.
In the result, the Calculated field returns the value of 3 as the smallest number and the value of 7 as the largest number.
Mind that the Math.min() method will return 0 if at least one of the number fields is empty. To handle the cases where one of the fields might be null (empty), you should use the below formula instead.
( Math.min( %number_one%, %number_two%, %number_three% ) == 0
?
(
( %number_one% + %number_two% + %number_three% - Math.max( %number_one%, %number_two%, %number_three% ) ) == 0
?
Math.max( %number_one%, %number_two%, %number_three% )
:
( %number_one% + %number_two% + %number_three% - Math.max( %number_one%, %number_two%, %number_three% ) )
)
:
Math.min( %number_one%, %number_two%, %number_three% ) )
In this formula, using ternary operators, we check if either of the number fields returns an empty value. If one of the fields is empty, we adjust the final result so that it returns the smallest of specified numbers. To find more information on ternary operators, check our tutorial on How to Use Conditional Formulas in the Calculated Field.
If one of the values is null, the formula will find the lowest value from those specified.
Exponentiation
The Math.pow() method raises the first specified number to the power of the second number.
To apply this method in the Calculated field, use the below formula:
(Math.pow(%number_one%, %number_two%))
In the above formula, the ‘number_one’ value stands for the name of the form field that provides the base number; the ‘number_two’ value stands for the exponent.
Insert the formula into the Calculated field to raise the value of the first specified field to the power of the second.
In our example, the first Number field contains the number 3; the second Number field contains the number 5. The expected result is 243.
That’s all; now you know how to use advanced math calculations with the help of the Calculated Field available in the JetFormBuilder plugins bundle.