VBA.VB6 - Rounding Function (greater or less than n digits)

December 2016





The Round() function provides incorrect rounding the numbers of in the following form:
(2a + 0,5) x 10^-n (Round half down)
By cons, the numbers of the form (2a - 0,5) x 10^-n are usually Round half up.
Author: Patrice33740
For example:

Round(0.15, 1) = 0.2
Round(0.25, 1) = 0.2
Round(0.35, 1) = 0.4
Round(0.45, 1) = 0.4

Rounding function given below does not have this anomaly:

Round(0.15, 1) = 0.2
Round(0.25, 1) = 0.3
Round(0.35, 1) = 0.4
Round(0.45, 1) = 0.5


Private Function Round(ByVal Number, ByVal Decimal)     
      Arrondi = Int(Number * 10 ^ Decimal + 1 / 2) / 10 ^ Decimal     
End Function


Related :

This document entitled « VBA.VB6 - Rounding Function (greater or less than n digits) » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.