Numeric Functions

Numeric functions include:

Numeric functions in QLingo can handle numbers with a maximum of 14 digits. Using numeric functions on numbers with 15 or more digits may cause incorrect results.

ABS Function

The ABS function gets the absolute value of the number expression.

Syntax:

Example:

ABS(-5) = 5

Floor Function

The Floor function floors the number expression.

Syntax:

Floor(number expression)

Example:

Floor(3.78) = 3

Ceil Function

The Ceil function ceils the number expression.

Syntax:

Ceil(number expression)

Example:

Ceil(3.12) = 4

FormatNumber Function 

The FormatNumber function formats the number in expression1 according to the format specification in expression2.

expression2 represents the input string using three special characters: “#”, “0”and “.” . You can also use other characters, such as the dollar sign ($). Any character other than “#”, “0”and “.” remains as it was in the format specification.

The dot divides the number into to parts: integral and fractional. The digits in each part are ordered as follows:

  • Integral part (to the left of the dot): expression2 adds digits from right to left (that is, from the smallest position to the largest position), depending on the number of “#” or “0” placeholders.

  • Fractional part (to the right of the dot): expression2 adds digits from left to right (that is, from the large position to the smallest position), depending on the number of “#” or “0” placeholders.

If expression2 does not include a dot, the input number (expression1) is treated as an integer and the fractional part is ignored.

The following table shows the FormatNumber Function  - special characters used by expression2.

 

Character

Description

#

Number sign.

Used as a placeholder for digits. If there are more placeholders than digits, they will be removed.

0

Zero.

Used as a placeholder for digits. If there are more placeholders than digits, they will appear as “0”.

.

Dot.

This character divides the number into two parts: integral (to the left of the dot) and fractional (to the right of the dot).

Syntax:

FormatNumber(expression1, expression2)

Example:

In the following example, expression2 defines nine placeholders, divided into groups of three, separated by commas. There are no placeholders for a fraction. The format specification ends with a dollar sign ($), which remains as-is, regardless of the input string. expression1 is an integer with five digits (10000), with no fractional section. In this case, there are enough placeholders for all digits and the result is “10,000$”:

FormatNumber (10000,"###,###,###$") = 10,000$

In the next example, expression2 defines only two placeholders for the integer and two placeholders for the fraction. expression1 includes both an integral part (1234) and a fractional part (.5). In this case, there are not enough placeholders for all digits in the integral part, only for the first two from the right: 4 and 3. Therefore, the result is “34.50”:

FormatNumber(1234.5, "00.00")= 34.50

Rand Function

The Rand function generates a random integer between 0 and the calculated integer value of the expression (not including). If the value of the expression is 1, a floating-point number between 0 and 1 (exclusive), will be returned.

Syntax:

Rand(expression)

Examples:

Rand(5)   can return a value of 1

Rand(1)   can return a value of 0.2376

Round Function

The Round function rounds off the calculated number value of expression1 as an integer, with expression2 as the number of precision digits.

Syntax:

Round(expression1,expression2)

Example:

Round(12.344, 2) returns the value of 12.34.