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

July 2017





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


Published by deri58. Latest update on November 5, 2010 at 07:57 AM by deri58.
This document, titled "VBA.VB6 - Rounding Function (greater or less than n digits)," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).