Round2BR
function Round2BR(aValue: Extended; aDP: Integer): Extended;
Example
procedure ScriptEvent(var Value: variant);
begin
Value := Round2BR(12.25, 1); // normally 12.2: scaled 122.5 ties to even 122
end;
Usage
Round2BR rounds at a decimal position by scaling through Double and applying Velox nearest-even Round.
Parameters
| Name | Type | Description |
|---|---|---|
aValue | Extended | Value to round. |
aDP | Integer | Decimal-place scale: positive for fractional digits, zero for units and negative for tens/hundreds while scaling remains valid. |
Returns
An Extended rescaled result. Under default nearest/even rounding:
| Call | Typical result |
|---|---|
Round2BR(12.25, 1) | 12.2 because 122.5 -> 122 |
Round2BR(12.35, 1) | potentially 12.4, subject to binary representation of the scaled midpoint |
Round2BR(155, -1) | 160 because 15.5 -> 16 |
Round2BR(145, -1) | 140 because 14.5 -> 14 |
Additional Technical Info
Round2BR scales an Extended value by a power of ten, deliberately assigns the scaled value to Delphi Real (the modern Double alias), rounds that Double to Int64 with System.Round, then divides by the scale.
Under the default rounding mode it uses banker's/nearest-even rounding. The intentional Double conversion changes precision before rounding and was added to address a particular decimal-boundary case in product code; it does not make binary floating point decimal-exact. The example is fictional and source-reviewed only.
Implementation
Nominator := Power(10, aDP);
x := aValue * Nominator; // x: Real (Double)
Result := Round(x) / Nominator;
Power is the native Velox wrapper over System.Math.Power. Round here is Delphi System.Round returning Int64, not the PascalScript Round wrapper with unchecked 32-bit narrowing.
Edge cases and quirks
- Midpoint behavior follows the active floating-point rounding mode. Nearest/even is the normal default but is not forced here.
- On Win32, assigning scaled
ExtendedtoRealloses 80-bit precision before rounding. On Win64,Extendedalready aliases Double. - Decimal literals and their scaled values may fall just above or below the mathematical midpoint in binary; the apparent last decimal digit alone is not a guarantee.
- Extreme positive
aDPcan overflow during scaling or pushRound(x)outsideInt64. Extreme negativeaDPcan underflow the scale and make final division invalid. - The
Powertarget-specific extreme-exponent behavior is inherited. - NaN/infinity and invalid conversions propagate according to Delphi's floating-point exception handling.
Performance and side effects
The function is pure and constant-time. It performs power/scaling, a precision conversion and an integer conversion on every call.
Related entries
Round2uses a non-standard six-tenths threshold.Round2Upuses text and midpoint-away-from-zero semantics for supported input.RoundAsdispatches to this function with tokenROUND2BR.
External references
Created 2026-07-15