Arithmetic operators

This article shows how to use type conversion rules for arithmetic operators in AL. The examples demonstrate operator usage and the effect of type conversion by the AL compiler. Examples are grouped by AL data types.

Unary arithmetic operators

The unary arithmetic operators in AL are prefix operators with the following syntax.

PrefixExpression = PrefixOperator Expression

The following table shows the data types for which the unary operators in AL are defined, and the resulting data types.

Unary operatorOptionIntegerDecimal
+integerintegerdecimal
-integerintegerdecimal

Binary arithmetic operators

The following table shows the data types for which the binary arithmetic operators are defined. The binary arithmetic operators in AL are all infix operators.

InfixExpression = LeftExpression InfixOperator RightExpression
OperatorBooleanByteCharOptionIntegerDecimalDateTimeTextCode
+NoYesYesYesYesYesYesYesYesYes
-NoYesYesYesYesYesYesYesNoNo
*NoYesYesYesYesYesNoNoNoNo
/NoYesYesYesYesYesNoNoNoNo
DIVNoYesYesYesYesYesNoNoNoNo
MODNoYesYesYesYesYesNoNoNoNo

In the table, Yes means that the operator can take at least one operand (left, right, or both) of the given type; No means that the operator can't be used with the given type.

The following table defines the valid uses of the binary arithmetic operators, and the resulting data types.

Definition of type conversion rules for the + operator

The + operatorBooleanByte/CharOptionIntegerDecimalDateTimeTextCode
Boolean
Byte/CharIntegerInteger (C)Integer (C)Decimal (C)
OptionIntegerInteger (C)Integer (C)Decimal (C)
IntegerInteger (C)Integer (C)Integer (C)Decimal (C)
DecimalDecimal (C)Decimal (C)Decimal (C)Decimal (C)
DateDate (A) (C)Date (A) (C)Date (A) (C)Date (A) (C) (D)
TimeTime(B) (C)Time(B) (C)Time(B) (C)Time(B) (C) (D)
TextTextText
CodeTextCode

(A) The operation isn't defined for the Date 0D.

(B) The operation isn't defined for the Time 0T.

(C) Overflow might occur.

(D) The operation isn't defined if the Decimal has a fractional part.

Example 1

This table illustrates type conversion in integer operator expressions.

OperatorNameExpressionResulting data type
+Time additionTime + IntegerTime
-Time differenceTime - TimeInteger

The time unit is milliseconds. If time is undefined (0T), a runtime error occurs.

Example 2

This table illustrates type conversion in text and code (String) operator expressions.

OperatorNameExpressionResulting data type
+ConcatenationText + TextText
+ConcatenationText + CodeText
+ConcatenationCode + TextText
+ConcatenationCode + CodeCode

AL operators
Boolean operators
Relational operators