Advertisement
ASP_Volume3 VB function enhancement #52699

Symmetric Arithmetic Rounding (Excel style rounding)

The Visual Basic functions CByte(), CInt(), CLng(), CCur() and Round() user a Banker's Rounding algorithm. For example, VBA.Round(0.15,1) = 0.2 **AND** VBA.Round(0.25,1) = 0.2. The following code uses Symmetric Arithmetic Rounding (similar to the Excel Worksheet Round function) where Round(0.15,1) = 0.2 and Round(0.25,1) = 0.3. Also, precision is enhanced by passing the 'Number' parameter as variant and using CDec within the routine. This helps circumvent floating point limitations. To see an excellent resource on different rounding procedures (the basis for this code) see Microsoft Article ID: Q196652.

AI

Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Codice sorgente
original-source
Public Function Round(Number As Variant, _
           Optional NumDigitsAfterDecimal As Long) As Variant
  If Not IsNumeric(Number) Then
    Round = Number
  Else
    Round = Fix(CDec(Number * (10 ^ NumDigitsAfterDecimal)) + 0.5 * Sgn(Number)) / _
        (10 ^ NumDigitsAfterDecimal)
  End If
End Function
Commenti originali (3)
Recuperato da Wayback Machine