mathLib.round

The system function mathLib.round rounds a number or expression to a nearest value (for example, to the nearest thousands) and returns the result.


mathLib.round syntax diagram

result

Any numeric or HEX item, as described in Mathematical (system words). The value produced by the rounding operation is converted to the format of result and returned in result.

The maximum supported length in this case is 31 rather than 32 because rounding occurs as follows:

  • Add five to the digit in result at a precision one higher than the precision of the result digit

  • Truncate the result

A numeric overflow occurs at run time if more than 31 digits are used in the calculation and if EGL cannot determine the violation at development time.

numericItem

Any numeric or HEX item, as described in Mathematical (system words).

numericExpression

A numeric expression other than simply a numeric item. If you specify an operator, you cannot specify a value for integer.

You cannot use mathLib.round with the remainder operator (%).

integer

An integer that determines the value to which the number is rounded:

  • If the integer is positive, the number is rounded to a nearest value equal to 10 to the power of integer. If integer is 3, for example, the number is rounded to the nearest thousands.

  • The same is true if the integer is zero or negative; in that case, the number is rounded to the specified number of decimal places.

If you do not specify integer, mathLib.round rounds to the number of decimal places in result.

The integer is defined as type INT or the following equivalent: type BIN with length 9 and no decimal places.

Examples

In the next example, item balance is rounded to the nearest thousand:

balance = 12345.6789;
rounder = 3;
balance = mathLib.round(balance, rounder);
// The value of balance is now 12000.0000

In the next example, a rounder value of -2 is used to round balance to two decimal places:

balance = 12345.6789;
rounder = -2;
balance = mathLib.round(balance, rounder);
// The value of balance is now 12345.6800

Related reference
Mathematical (system words)