Bewerken

Delen via


System.Round(Decimal [, Decimal] [, Text]) Method

Version: Available or changed with runtime version 1.0.

Rounds the value of a numeric variable.

Syntax

NewNumber :=   System.Round(Number: Decimal [, Precision: Decimal] [, Direction: Text])

Note

This method can be invoked without specifying the data type name.

Parameters

Number
 Type: Decimal
The number that you want to round.

[Optional] Precision
 Type: Decimal
This optional parameter determines the precision used when rounding. If you do not specify a Precision parameter, then the following steps are used to specify the precision:

  1. The method ReadRounding in Codeunit 45, ReadRounding, is called. ReadRounding returns a decimal value that is the precision. By default, the ReadRounding method returns the Amount Rounding Precision field from the GLSetup table.
  2. If you have customized Codeunit 45 and it does not implement the ReadRounding method, then the precision is specified as 2 digits after the decimal.

[Optional] Direction
 Type: Text
This optional parameter specifies how to round the Number parameter. The default rounding method is '='. The following are the options for rounding:

  • '=' rounds up or down to the nearest value (default). Values of 5 or greater are rounded up. Values less than 5 are rounded down.
  • '>' rounds up
  • '<' rounds down

Return Value

NewNumber
 Type: Decimal

Example

This example shows how to use the Round method.

var
    DecimalToRound: Decimal;
    Direction: Text;
    Precision: Decimal;
    Result: Decimal;
    Text00: Label 'Round(%1, %2, %3) returns %4.';
begin
    DecimalToRound := 1234.56789;  
    Direction := '>';  
    Precision := 0.001;  
    Result := Round(DecimalToRound, Precision, Direction);  
    Message(Text000, Format(DecimalToRound,0,1), Precision, Direction, Result);  
end;

On a computer that has the regional format set to English (United States), the message window displays the following:

Round(1234.56789, 0.001, '>') returns 1,234.568

The following table displays some additional Round examples.

Number Precision Direction Rounded number
1234.56789 100 = 1200
1234.56789 10 = 1230
1234.56789 1 = 1234
1234.56789 0.1 = 1234.6
1234.56789 0.001 = 1234.568
1234.56789 0.001 < 1234.567
1234.56789 0.001 > 1234.568
-1234.56789 100 = -1200
-1234.56789 10 = -1230
-1234.56789 1 = -1234
-1234.56789 0.1 = -1234.6
-1234.56789 0.001 = -1234.568
-1234.56789 0.001 < -1234.567
-1234.56789 0.001 > -1234.568

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.

System Data Type
Get Started with AL
Developing Extensions