SetRoundMode (Delphi)
Contents
Description
The application below involves a form containing a TMemo control and a TButton control. The results of calculations appear in the form's Memo control at run time.
Clicking the button in this complete VCL application demonstrates that according to the rounding mode, Delphi rounds certain values differently.
The values 5.7 and -1.3 are used here:
- 5.7 is rounded to:
- 5.00 if rmDown or rmTruncate is set
- 6.00 if rmUp or rmNearest is set
- -1.3 is rounded to:
- -2.00 if rmDown is set
- -1.00 if rmUp, rmNearest, or rmTruncate is set
As you can see, when SetRoundMode() is given rmDown, applying Round() produces the nearest whole number that is less than or equal to value passed as parameter, for 5.7, the return value is 5.00 and for -1.3, this value is -2.00.
When rmUp is specified, the nearest whole number that is greater than equal to the parameter is returned, namely 6.00 and -1.00 respectively.
For rmNearest, the closest whole number is returned without regard it for being less than or greater than the value passed to Round().
With rmTruncate, Round() simply return the value of the parameter passed with the fractional portion removed.
Code
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.Math;
procedure TForm1.Button1Click(Sender: TObject);
var
OldRM: TRoundingMode;
TestValue: Real;
procedure RecordRound(const TextStr: String; Value: Real);
{ Calls Round() and shows its value in the Memo control.
Affected by the value passed to SetRoundMode(). }
var RetVal: Real;
begin
RetVal := Round(Value);
Memo1.Lines.Add(Format('%-15s RetVal=%f', [TextStr, RetVal]));
end;
begin
OldRM := GetRoundMode; { Save the original setting for the Round Mode }
TestValue := 5.7; { First test value }
Memo1.Lines.Add(Format('Value=%f', [TestValue]));
SetRoundMode(rmDown);
RecordRound('rmDown #1', TestValue);
SetRoundMode(rmUp);
RecordRound('rmUp #2', TestValue);
SetRoundMode(rmNearest);
RecordRound('rmNearest #3', TestValue);
SetRoundMode(rmTruncate);
RecordRound('rmTruncate #4', TestValue);
Memo1.Lines.Add(''); { Adds a blank line to the memo }
TestValue := -1.3; { Second test value }
Memo1.Lines.Add(Format('Value=%f', [TestValue]));
SetRoundMode(rmDown);
RecordRound('rmDown #5', TestValue);
SetRoundMode(rmUp);
RecordRound('rmUp #6', TestValue);
SetRoundMode(rmNearest);
RecordRound('rmNearest #7', TestValue);
SetRoundMode(rmTruncate);
RecordRound('rmTruncate #8', TestValue);
SetRoundMode(OldRM); { Restore the original Rounding Mode }
end;
end.
Uses
- System.Math.SetRoundMode ( fr | de | ja )
- Vcl.StdCtrls.TMemo.Lines ( fr | de | ja )
- System.Classes.TStrings.Add ( fr | de | ja )