Floating-Point Rounding Routines

From RAD Studio
Jump to: navigation, search

Go Up to Using Floating-Point Routines

Many of the rounding routines in the Delphi Run-Time Library (RTL) use the currently set rounding mode for the FPU (floating-point unit) or SSE register, for 32-bit or 64-bit, respectively. Some RTL routines have their own rounding mode or algorithm. The tables on this page present more detail about some of the various rounding routines.

Rounding Routines

The following table summarizes the rounding functions available in the RTL:

Name Argument type Return type Has digit parameter? Range of digit parameter Rounding mode Notes

System.Round
(built-in)

Extended

Int64

Current RoundMode setting

System.Trunc
(built-in)

Extended

Int64

Always rmTruncate (Truncate toward Zero)

Same as Int. Return type is integer.

System.Int
(built-in)

  • Extended for 32bit
  • Double for 64bit
  • Extended for 32bit
  • Double for 64bit
Always rmTruncate (Truncate toward Zero) Same as Trunc. Return type is floating-point number.

System.Math.RoundTo

Extended

Extended Yes -20 .. +20

no default value.

Always rmNearest (Banker's round)

System.Math.SimpleRoundTo

Single
Double
Extended

Single
Double
Extended

Yes

-37 .. +37

-2 is default value.

Always "Round half away from zero" SimpleRoundTo is the traditional "Round off" that was learned at school.

System.Math.Ceil

Single
Double
Extended

Integer

Always rmUp (the smallest following integer number)
System.Math.Floor Single
Double
Extended
Integer Always rmDown (the largest previous integer number)
Data.FmtBcd.BCDRoundTo TBcd TBcd Yes Integer Current RoundMode

FMTBcd.BCDRoundAt
(Deprecated)

String String Yes SmallInt Always "Round half away from zero Deprecated function.  Use BcdRoundTo.

Examples of Positive Numbers

The following table gives examples of rounding positive numbers:

Name Rounding Mode 0.4 0.5 0.6 1.4 1.5 1.6

System.Round(x)

rmNearest 0 0 1 1 2 2
rmDown 0 0 0 1 1 1
rmUp 1 1 1 2 2 2
rmTruncate 0 0 0 1 1 1

System.Trunc(x)

0 0 0 1 1 1

System.Int(X)

0.0 0.0 0.0 1.0 1.0 1.0

Math.RoundTo(x, 0)

0.0 0.0 1.0 1.0 2.0 2.0

Math.SimpleRoundTo(x,0)

0.0 1.0 1.0 1.0 2.0 2.0

Math.Ceil(x)

1 1 1 2 2 2

Math.Floor(x)

0 0 0 1 1 1

FMTBcd.BCDRoundTo(x,0)

rmNearest

0 0 1 1 2 2

rmDown

0 0 0 1 1 1

rmUp

1 1 1 2 2 2

rmTruncate

0 0 0 1 1 1

FmtBcd.RoundAt(x,0)
(Deprecated)

'0' '1' '1' '1' '2' '2'

Examples of Negative Numbers

The following table gives examples of rounding negative numbers:

Name Rounding Mode -0.4 -0.5 -0.6 -1.4 -1.5 -1.6

System.Round(x)

rmNearest 0 0 -1 -1 -2 -2
rmDown -1 -1 -1 -2 -2 -2
rmUp 0 0 0 -1 -1 -1
rmTruncate 0 0 0 -1 -1 -1
System.Trunc(x) 0 0 0 -1 -1 -1
System.Int(X) 0.0 0.0 0.0 -1.0 -1.0 -1.0
Math.RoundTo(x,0) 0.0 0.0 -1.0 -1.0 -2.0 -2.0
Math.SimpleRoundTo(x,0) 0.0 -1.0 -1.0 -1.0 -2.0 -2.0
Math.Ceil(x) 0 0 0 -1 -1 -1
Math.Floor(x) -1 -1 -1 -2 -2 -2

FMTBcd.BCDRoundTo(x,0)

rmNearest 0 0 -1 -1 -2 -2
rmDown -1 -1 -1 -2 -2 -2
rmUp 0 0 0 -1 -1 -1
rmTruncate 0 0 0 -1 -1 -1
FmtBcd.RoundAt(x,0)

(Deprecated)

'-0' '-1' '-1' '-1' '-2' '-2'

Examples of the Digit Parameter with RoundTo and SimpleRoundTo

The digit (D) indicates the power of ten to which you want the given value to be rounded. The digit can be any value in the range from –20 through 20. The examples here use D = 0, -1 ... -5.


Name 0 -1 -2 -3 -4 -5
Math.RoundTo(2.15625, D) 2.0 2.2 2.16 2.156 2.1562 2.15625
Math.SimpleRoundTo(2.15625, D) 2.0 2.2 2.16 2.156 2.1563 2.15625

See Also

Code Examples