Name
Round Function
Syntax
function Round(X: Floating-point type): Int64;Description
The Round function rounds off a floating-point
value to an integer. Round is not a real function.
Tips and Tricks
The
Roundfunction uses the prevailing round mode in the floating-point control word. By default, Delphi sets this to round-towards-even, also known as “banker’s rounding.” Numbers are rounded to the closest integer, and numbers that fall exactly between two integers are rounded off to an even number.If you change the floating-point control word, you also change the behavior of
Roundand all functions that depend onRound. If you want to choose a different round-off mode, change the floating-point control word temporarily, as shown below.
// Round up towards positive infinity.
function RoundUp(X: Extended): Int64;
const
RoundUpCW: Word = $1B32; // same as Default8087CW, except round up
var
OldCW: Word;
begin
OldCW := Default8087CW;
try
Set8087CW(RoundUpCW);
Result := Round(X);
finally
Set8087CW(OldCW);
end;
end;If
Xis an integer, the compiler eliminates the call toRoundand simply returnsX.If
Xis aVariant, Delphi automatically converts it to a floating-point number and rounds it off.The compiler does not accept an
Int64argument, but there is no reason to callRoundforInt64.If
Xis infinity or NaN,Roundreports runtime error 6 (EInvalidOp).
See Also
| Int Function, Trunc Function |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access