Function Math Reals Class

RoundToPrecision

Round the number vNumber to the nearest vPrecision.

Signature (Visual Basic / VBA)

Public Function RoundToPrecision( _
      ByVal vNumber As Variant _
    , Optional ByVal vPrecision As Variant _
    ) As Variant

Code Examples

    RoundToPrecision(45.3) = 45
    RoundToPrecision(45.5) = 46 ' #2
    RoundToPrecision(45.7) = 46
    RoundToPrecision(-45.7) = -46
    RoundToPrecision(-45.5) = -45 ' #5
    RoundToPrecision(-45.3) = -45
    RoundToPrecision(-45.37, .1) = -45.4
    RoundToPrecision(-12.3456, .01) = -12.35
    Floor(vNumber / vPrecision + .5) * vPrecision

Parameters

Parameter Description
vNumber The number that is rounded either up or down to the nearest vPrecision. Function returns Null if vNumber is Null or cannot be fixed up to a number.
vPrecision The nearest number to which vNumber is rounded. vPrecision defaults to 1 (zero) if it is missing or Null or cannot be fixed up to a String. With a precision of 1 (one), vNumber will be rounded to the nearest integer; with a precision of .1, vNumber will be rounded to the nearest tenth, etc.

Notes & Version History

Note When vNumber is exactly half-way between the range the numbers, it is always rounded up, as in examples #2 and #5.
v1.3 BugFix This function was returning incorrect results in prior versions of Entisoft Tools. Both the Function and its Definition have been corrected.
v1.5 Change This function has been renamed from Round to RoundToPrecision, in order to avoid confusion with the new Round Function in Visual Basic 6.0. The Round function in Visual Basic 6.0 is the same as the RoundToDigits Function in Entisoft Tools.