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.

**“Price” field of Auto items****“Price” field of Adjustment item**

Adjustment items are almost the same as Auto items, but they are components to be used in discounts and premiums (calculation of the percentage of total amount).**“Quantity” field of AutoQuantity item**

The AutoQuantity item is a component for calculating a different quantity using the quantity entered in the quantity item or slider.

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.

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

- Numbers entered in Quantity items and Sliders
- The result of an AutoQuantity item calculation
- Subtotal of the quotation (total excluding tax)

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:

`100.5`

— just a number is also an expression`-100 * (50 + 2)`

— you can use arithmetic operations and parentheses.`*`

is multiplication.-
`2 * X + 10`

— you can use variables. -
`(1 + z) + 2 * (3 / a + z)`

—`a`

and`z`

are variables.`z`

is used twice.`/`

is a division -
`(1+z)+2* (3/ a+z)`

— can be used with or without spaces -
`ABS(-10.15256)`

— you can use the functions.`ABS`

is a function to find absolute values.

Note that you can’t write unary minus in the expression.

Here are some examples:

`-1`

— OK. It’s just a number-
`-X`

— No good. There is a unary minus before the variable X -
`3 * -(1 + 2)`

— NG. There is a unary minus before the parentheses -
`3 * -1 * (1 + 2)`

— OK. The above expression can be rewritten like this -
`3 - (1 + 2)`

— This is also OK; this minus is not a unary minus

### 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:

`X`

— is a variable-
`x`

— same variable as above -
`abc`

— it doesn’t have to be a single letter, as long as it’s consecutive -
`ABc`

— same variable as above -
`x1`

— you can use any number after the second letter -
`1x`

— Do not use a number for the first letter

## Operators and Functions

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

`a + b` | Addition`1 + 2` -> `3` |

`a - b` | Subtraction`3 - 1` -> `2` |

`a * b` | Multiplication`2 * 3` -> `6` |

`a / b` | Division`5 / 2` -> `2.5` |

`a ^ b` | Power`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.