Use the Round function

Completed

The Round function rounds the value of a numeric variable. The syntax is as follows:

NewNumber := Round(Number [, Precision] [, Direction])

The optional Precision parameter determines the precision that is used when you are rounding. If you do not specify a Precision parameter, then the ReadRounding function in codeunit 45 (Auto Format) is called. The ReadRounding function returns a decimal value that is the precision. By default, the ReadRounding function returns the Amount Rounding Precision field from the General Ledger Setup table.

The optional Direction parameter specifies how to round the Number parameter. The default rounding method is '='. The options for rounding are:

  • '=' - Rounds up or down to the nearest value (default). Values of five or greater are rounded up. Values less than five are rounded down.

  • '>' - Rounds up

  • '<' - Rounds down

The following example shows how to use the Round function.

var
    DecimalToRound: Decimal;
    Direction: Text;
    Precision: Decimal;
    Result: Decimal;
    RoundTxt: Label 'Round(%1, %2, %3) return %4.';
begin
    DecimalToRound := 1234.56789;
    Direction := '>';
    Precision := 0.001;
    Result := Round(DecimalToRound, Precision, Direction);
    Message(RoundTxt, Format(DecimalToRound, 0, 1), Precision, Direction, Result);
    // Output on a computer with regional format set to English (United States)
    // Round(1234.56789, 0.001, '>') returns 1,234.568
end;

When you round down ('<') a negative number, such as -1234.56789, it is rounded down to -1234.567. However, -1234.567 is a mathematically greater value than -1234.56789.

When you round up ('>') a negative number, such as -1234.56789, it is rounded up to -1234.568. However, -1234.568 is a mathematically smaller value than -1234.56789.