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 Round function 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 Round and all functions that depend on Round. 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 X is an integer, the compiler eliminates the call to Round and simply returns X.

  • If X is a Variant, Delphi automatically converts it to a floating-point number and rounds it off.

  • The compiler does not accept an Int64 argument, but there is no reason to call Round for Int64.

  • If X is infinity or NaN, Round reports runtime error 6 (EInvalidOp).

See Also

Int Function, Trunc Function

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.