Trunc function
Truncates a real number to an integer.
var
S, T: string;
begin
Str(1.4:2:1, T);
S := T + ' Truncs to ' + IntToStr(Trunc(1.4)) + #13#10;
Str(1.5:2:1, T);
S := S + T + ' Truncs to ' + IntToStr(Trunc(1.5)) + #13#10;
Str(-1.4:2:1, T);
S := S + T + ' Truncs to ' + IntToStr(Trunc(-1.4)) + #13#10;
Str(-1.5:2:1, T);
S := S + T + ' Truncs to ' + IntToStr(Trunc(-1.5));
MessageDlg(S, mtInformation, [mbOk], 0);
end;
function Trunc(X: Extended): Int64;
Description
The Trunc function truncates a real-type value to an integer-type value. X is a real-type expression. Trunc returns an Int64 value that is the value of X rounded toward zero.
If the truncated value of X is not within the Int64 range, an EInvalidOp exception is raised.
_____________________________
Int function
Returns the integer part of a real number.
var
R: Real;
begin
R := Int(123.456); { 123.0 }
R := Int(-123.456); { -123.0 }
end;
function Int(X: Extended): Extended;
Description
Int returns the integer part of X; that is, X rounded toward zero. X is a real-type expression.
Round function
Returns the value of X rounded to the nearest whole number.
var
S, T: string;
begin
Str(1.4:2:1, T);
S := T + ' rounds to ' + IntToStr(Round(1.4)) + #13#10;
Str(1.5:2:1, T);
S := S + T + ' rounds to ' + IntToStr(Round(1.5)) + #13#10;
Str(-1.4:2:1, T);
S := S + T + ' rounds to ' + IntToStr(Round(-1.4)) + #13#10;
Str(-1.5:2:1, T);
S := S + T + ' rounds to ' + IntToStr(Round(-1.5));
MessageDlg(S, mtInformation, [mbOk], 0);
end;
______________________________________
function Round(X: Extended): Int64;
Description
The Round function rounds a real-type value to an integer-type value.
X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number.
If the rounded value of X is not within the Int64 range, a run-time error is generated, which can be handled using the EInvalidOp exception.