Function Math Reals Class

RoundToDigits

Round the number vNumber to vDigits digits of accuracy.

Signature (Visual Basic / VBA)

Public Function RoundToDigits( _
      ByVal vNumber As Variant _
    , Optional ByVal vDigits As Variant _
    ) As Variant

Code Example

    RoundToDigits(45.3) = 45
    RoundToDigits(45.5) = 46 ' #2
    RoundToDigits(45.7) = 46
    RoundToDigits(-45.7) = -46
    RoundToDigits(-45.5) = -45 ' #5
    RoundToDigits(-45.3) = -45
    RoundToDigits(-45.37, 1) = -45.4
    RoundToDigits(-12.3456, 2) = -12.35

Parameters

Parameter Description
vNumber The number that is rounded either up or down so that its precision is vDigits digits. Function returns Null if vNumber is Null or cannot be fixed up to a number.
vDigits The number of fractional digits to which vNumber is rounded. vDigits defaults to 0 (zero) if it is missing or Null or cannot be fixed up to a String. With a precision of 0 (zero), vNumber will be rounded to the nearest integer; with a precision of 1 (one), 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.
Note Function will return Null instead of generating an error if any of its calculations cause Overflow or other types of errors.
v1.5 Note The Round function in Visual Basic 6.0 is the same as this function.