meta data for this page
  •  

round, i64round

Library: fbudf

Added in: 1.0 (Win), 1.5 (Linux)

Changed in: 1.5, 2.1.3

Better alternative: Internal function ROUND()

Description

These functions return the whole number that is nearest to their (scaled numeric/decimal) argument. They do not work with floats or doubles.

Result type: INTEGER / NUMERIC(18,4)

Syntax

round (number)
i64round (bignumber)

Caution: Halves are always rounded upward, i.e. away from zero for positive numbers and toward zero for negative numbers. For instance, 3.5 is rounded to 4, but -3.5 is rounded to -3. The internal function ROUND, available since Firebird 2.1, rounds all halves away from zero.

Bug alert: In versions 2.1, 2.1.1 and 2.1.2, these functions are broken for negative numbers:

  • Anything between 0 and -0.6 (that's right: -0.6, not -0.5) is rounded to 0.
  • Anything between -0.6 and -1 is rounded to +1 (plus 1).
  • Anything between -1 and -1.6 is rounded to -1.
  • Anything between -1.6 and -2 is rounded to -2.
  • Etcetera.

Fixed for 2.1.3

Declarations

In Firebird 1.0.x, the entry point for both functions is round:

DECLARE EXTERNAL FUNCTION Round
  INT BY DESCRIPTOR, INT BY DESCRIPTOR
  RETURNS PARAMETER 2
  ENTRY_POINT 'round' MODULE_NAME 'fbudf'

DECLARE EXTERNAL FUNCTION i64Round
  NUMERIC(18,4) BY DESCRIPTOR, NUMERIC(18,4) BY DESCRIPTOR
  RETURNS PARAMETER 2
  ENTRY_POINT 'round' MODULE_NAME 'fbudf'

In Firebird 1.5, the entry point has been renamed to fbround:

DECLARE EXTERNAL FUNCTION Round
  INT BY DESCRIPTOR, INT BY DESCRIPTOR
  RETURNS PARAMETER 2
  ENTRY_POINT 'fbround' MODULE_NAME 'fbudf'

DECLARE EXTERNAL FUNCTION i64Round
  NUMERIC(18,4) BY DESCRIPTOR, NUMERIC(18,4) BY DESCRIPTOR
  RETURNS PARAMETER 2
  ENTRY_POINT 'fbround' MODULE_NAME 'fbudf'

If you move an existing database from Firebird 1.0.x to 1.5 or higher, drop any existing *round and *truncate declarations and declare them anew, using the updated entry point names. From Firebird 2.0 onward you can also perform this update with ALTER EXTERNAL FUNCTION.