Using expressions to calculate unit price and quantity in a form

Starting with v2.1, AForms allows you to use expressions for unit price and quantity.
If you write an expression, the calculation will be done in the form, and you can change the unit price and quantity on the fly.

Starting with v2.1, AForms allows you to use expressions for unit price and quantity.
If you write an expression, the calculation will be done in the form, and you can change the unit price and quantity on the fly.
In this article, we will explain how to use expressions.
Information on AForms v2.1.8.

Where you can write expressions

You can write the expression in the following places.

If you want to calculate the unit price, you can use the Auto item, and if you want to calculate the quantity, you can use the AutoQuantity item.

Using variables in expressions

You can use variables in expressions.
If you write a word that looks like a variable in an expression, it will be interpreted as a variable and you will be able to link that variable to the actual value.

When you enter an expression in the input field and then remove the focus, the variable will be displayed below the input field.

The values that can be associated with variables are as follows:

You can use any number of variables in an expression, any number of times in an expression.

How to write an expression (grammar)

You can write numbers, variables, operators, and function applications in an expression. You can also write parentheses.
Here are some examples:

Note that you can’t write unary minus in the expression.
Here are some examples:

Characters that can be used in variables

Consecutive one-byte alphanumeric characters are interpreted as variables. However, you cannot use a number as the first character of a variable.
The alphabet is not case-sensitive.
Here are some examples:

Operators and Functions

The following operators and functions are available, similar to those in Excel.

a + bAddition
1 + 2 -> 3
a - bSubtraction
3 - 1 -> 2
a * bMultiplication
2 * 3 -> 6
a / bDivision
5 / 2 -> 2.5
a ^ bPower
2 ^ 3 -> 8
IFERROR(a, b)Recovering from an error.
If a is not an error, then a. If an error, then b.
IFERROR(1 + 1, 3) -> 2
IFERROR(1 / 0, 3) -> 3
ROUND(a, b)Rounding
ROUND(41.82, 0) -> 42
ROUND(41.82, 1) -> 41.8
ROUNDUP(a, b)Round up (to positive/negative infinity)
ROUNDUP(41.82, 0) -> 42
ROUNDUP(41.82, 1) -> 41.9
ROUNDDOWN(a, b)Round down (to zero)
ROUNDDOWN(41.82, 0) -> 41
ROUNDDOWN(41.82, 1) -> 41.8
TRUNC(a, b)Round down (to zero)
TRUNC(41.82, 1) -> 41.8
TRUNC(-41.82, 1) -> -41.8
If b is omitted, then 0 is assumed.
TRUNC(41.82) -> 41
INT(a)Round down (to negative infinity)
INT(41.82) -> 41
INT(-41.82) -> -42
ABS(a)Absolute value
ABS(2) -> 2
ABS(-2) -> 2
SIGN(a)Sign of value
SIGN(2) -> 1
SIGN(-2) -> -1
SIGN(0) -> 0
QUOTIENT(a, b)The integer part of a division
QUOTIENT(7, 2) -> 3
QUOTIENT(10.5, 5) -> 2
MOD(a, b)Reminder part of a division
MOD(7, 2) -> 1
MOD(10.5, 5) -> 0.5
MIN(a, b, ...)Minimum value. You can give 1 to 255 parameters.
MIN(1, 5) -> 1
MIN(1, 5, -1) -> -1
MAX(a, b, ...)Maximum value. You can give 1 to 255 parameters.
MIN(1, 5) -> 5
MIN(1, 5, 10) -> 10
SWITCH(a, m1, e1, ...)Conditional branching. Following a, specify multiple pairs of branches consisting of condition m and expression e. The result of the calculation is e paired with m, which matches with a.
SWITCH(0, 0, 10, 1, 11) -> 10
SWITCH(1, 0, 10, 1, 11) -> 11
If a does not match any branch, an error will occur.
SWITCH(2, 0, 10, 1, 11) -> ERROR
As the last parameter, you can also specify “an expression for the case where no branch matches”.
SWITCH(2, 0, 10, 1, 11, 100) -> 100
SWITCH(0, 0, 10, 1, 11, 100) -> 10
The maximum number of parameters is 255, so you can create up to 126 branches.

Caution

What happens if an error occurs in the calculation?

If an error occurs in the calculation, the related detail line will be excluded from the quotation.
For example, if you write 1 / 0 in the price field of an automatic item, the calculation will result in an error because it includes division by zero. Then, the detail line of the automatic item that should have been included in the estimate will be excluded from the quotation.

Note that in the preview of the form, you will be alerted if there is an error in the calculation.
In production, which is not a preview, nothing will happen on the screen even if an error occurs in the calculation. However, if a customer submits the form, he will be alerted of an error.

Limitations of Computation

With the ability to use expressions, there will be more and more situations where AForms will handle large numbers.
How large a number AForms can handle will be affected by the environment in which the software runs.
As far as the author has been able to find out, there seems to be a problem when the number exceeds 12 digits (trillionths).
AForms takes care of problems that appear during the calculation of an expression, but may not be aware of problems that appear elsewhere. In that case, incorrect numbers may be displayed.
The boundary is roughly 12 digits (trillionths). Please be careful to make sure that your form does not go beyond this.

0 0